Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add the YouTube player in my SupportFragment?

My XML code:

<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>

Code in Fragment

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();

    }
}

In Main activity:

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

Error when I try to open my Fragment in Drawer:

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)

like image 322
RussellGk Avatar asked Nov 09 '22 12:11

RussellGk


1 Answers

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;
    }

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();
    }

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);
    }
like image 87
Wilhelm Sorban Avatar answered Nov 14 '22 23:11

Wilhelm Sorban