无法将 youtube 播放器片段添加到布局

cannot add youtube player fragment to layout

我正在尝试将 youtubeplayersupportfragment 添加到我的片段内的布局中。 在将片段添加到布局时它说 “无法将 youtubeplayersupportfragment 转换为片段。 这是我的代码

public class combofrag extends Fragment implements View.OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_combofrag, container, false);
    YouTubePlayerSupportFragment youTubePlayerFragment = new YouTubePlayerSupportFragment();

    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.youtube_view1, youTubePlayerFragment).commit();

    youTubePlayerFragment.initialize(Config.YOUTUBE_API_KEY, new YouTubePlayer.OnInitializedListener() {

        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
            if (!wasRestored) {
                player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
                player.loadVideo("mI8HWnkWk7c");
                player.play();
            }
        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult error) {
            // YouTube error
            String errorMessage = error.toString();
            Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
            Log.d("errorMessage:", errorMessage);
        }
    });
    return view;
}

这一行有错误

transaction.add(R.id.youtube_view1, youTubePlayerFragment).commit();

您的 child fragment managerandroid.app.FragmentManager 类型,而您的 YouTubePlayerSupportFragmentandroid.support.v4(or v7 etc).app.Fragment 类型。您不能将 fragment 转换为 support fragment

将您的 combofrag 更改为 support fragment 或将您的 YouTubePlayerSupportFragment 更改为 fragment 以解决此冲突。

我更愿意使用 support fragment 而不是只使用 fragment.

希望这会有所帮助。