选项卡活动内的Youtube播放器导致此错误

时间:2017-03-22 14:46:51

标签: java android youtube

我有标签活动,我希望在第三个标签上显示youtube视频。

这是我的java:

public class FragmentTutorial extends YouTubePlayerFragment {

    YouTubePlayerView youTubePlayerView;
    YouTubePlayer.OnInitializedListener onInitializedListener;

    public FragmentTutorial() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        youTubePlayerView = (YouTubePlayerView) youTubePlayerView.findViewById(R.id.tutorial_youtube_player);

        onInitializedListener = new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                youTubePlayer.loadVideo("3LiubyYpEUk");
            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

            }
        };

        youTubePlayerView.initialize(PlayerConfig.API_KEY, onInitializedListener);

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tutorial, container, false);
    }

}

这是我的XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.FragmentTutorial">

    <com.google.android.youtube.player.YouTubePlayerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tutorial_youtube_player" />

</FrameLayout>

这是我的MainActivity:

class FragmentAdapter extends FragmentPagerAdapter {

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return new FragmentHome();
                case 1:
                    return new FragmentCatalog();
                case 2:
                    return new FragmentTutorial();
            }
            return null;
        }

        @Override
        public int getCount() {
            return 3;
        }


        @Override
        public CharSequence getPageTitle(int position) {
            switch (position){
                //
                //Your tab titles
                //
                case 0: return "Home";
                case 1: return "Catalog";
                case 2: return "Tutorial";
                default: return null;
            }
        }
    }

当我尝试运行此代码时,出现以下错误:

Error:(80, 28) error: incompatible types: FragmentTutorial cannot be converted to Fragment

我在这里错过了什么?谢谢......

1 个答案:

答案 0 :(得分:2)

可能是由错误的Fragment类引起的,YouTubePlayerFragment扩展android.app.FragmentFragmentPagerAdapter需要android.support.v4.app.Fragment,而且无法转换

修改

尝试使用android.support.v13.app.FragmentPagerAdapter代替android.support.v4.app.FragmentPagerAdapter(其他片段应扩展android.app.Fragment