如何在我的SupportFragment中添加YouTube播放器? [英] How to add the YouTube player in my SupportFragment?

查看:176
本文介绍了如何在我的SupportFragment中添加YouTube播放器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    style="@style/Container.MainBackground"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
        android:layout_alignParentTop="true"
        android:id="@+id/youtube_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>

片段中的代码

public class VideoFragment extends YouTubePlayerSupportFragment implements YouTubePlayer.OnInitializedListener {

    static private final String DEVELOPER_KEY = "MyKey";
    static private final String VIDEO = "ToMpzhdUD1Q";
    static private final String VIDEO1 = "K77avo920Jc";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View videoView = inflater.inflate(R.layout.video_fragment, container, false);
        getActivity().setTitle("Youtube");

        YouTubePlayerSupportFragment youTubePlayerSupportFragment = new YouTubePlayerSupportFragment();
        youTubePlayerSupportFragment.initialize(DEVELOPER_KEY, this);

        return videoView;
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {

        List<String> list = new ArrayList<>();
        list.add(VIDEO);
        list.add(VIDEO1);
        youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
        youTubePlayer.cueVideos(list);

    }

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

        Toast.makeText(getContext(), "FAIL!" + youTubeInitializationResult.toString(), Toast.LENGTH_LONG)
                .show();

    }
}

在主要活动中:

getSupportFragmentManager().beginTransaction().replace(R.id.main_container,fragment).addToBackStack(null).commit();

尝试在抽屉中打开片段时出错:

Error when I try to open my Fragment in Drawer:

java.lang.NullPointerException:尝试调用虚拟方法'void com.google.android.youtube.player.YouTubePlayerView.a()"为null 对象引用位于 com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart(未知 来源)

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.youtube.player.YouTubePlayerView.a()' on a null object reference at com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart(Unknown Source)

推荐答案

此处的问题是您覆盖了超类 YouTubePlayerSupportFragment onCreateView()方法该方法不是抽象的,但实际上具有实现,如所示:

The issue here is that you overwrite the onCreateView() method from the super class YouTubePlayerSupportFragment, a method which is not abstract, and which actually has an implementation, as seen:

    public View onCreateView(LayoutInflater var1, ViewGroup var2, Bundle var3) {
        this.c = new YouTubePlayerView(this.getActivity(), (AttributeSet)null, 0, this.a);
        this.a();
        return this.c;
    }

实际的变量名和类型不在此答案的范围内. 此处重要的是 YouTubePlayerView 在此处实例化,并且您覆盖了该方法,因此当在 onStart()中调用时, YouTubePlayerView 为空.方法(也可以在 YouTubePlayerSupportFragment 中找到).

The actual variable names, and types are beyond the scope of this answer. What matters here is that YouTubePlayerView is getting instantiated here, and you overwrite the method, so YouTubePlayerView is null, when called in the onStart() method (also found in YouTubePlayerSupportFragment).

    public void onStart() {
        super.onStart();
        this.c.a();
    }

因此,基本上,您唯一需要做的就是实例化 VideoFragment 类,如果要使用实际的视频(而不是黑盒),则需要在以下位置初始化该片段:创建时间(例如:)

Therefore, basically, the only things you need, is to instantiate your VideoFragment class, and, if you want the actual video, instead of a black box, you need to initialize the fragment, at the time of its creation, (like this:)

    public VideoFragment()
    {
        this.initialize("yourAPIKeyHere", this);
    }

这篇关于如何在我的SupportFragment中添加YouTube播放器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆