【问题标题】:Youtube Player Inside Tabbed Activity Cause This Error选项卡式活动中的 Youtube 播放器导致此错误
【发布时间】:2023-03-19 16:38:01
【问题描述】:

我有选项卡式活动,我想在第三个选项卡上播放 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

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

【问题讨论】:

    标签: java android youtube


    【解决方案1】:

    可能是Fragment类错误,YouTubePlayerFragment扩展android.app.FragmentFragmentPagerAdapter需要android.support.v4.app.Fragment这个不能转换

    编辑:

    尝试使用android.support.v13.app.FragmentPagerAdapter 而不是android.support.v4.app.FragmentPagerAdapter(其他片段应该扩展android.app.Fragment

    【讨论】:

    • 知道如何在标签片段中安装 youtube 播放器吗?因为大多数教程建议根据需要使用extends Youtube*** 而不是extends fragment
    • 我有import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter;,我应该把它们都改成v13吗?
    • FragmentPagerAdapter 应该来自 v13
    猜你喜欢
    • 2017-08-14
    • 2013-04-23
    • 2016-12-22
    • 1970-01-01
    • 2020-02-18
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    相关资源
    最近更新 更多