Skip to content Skip to sidebar Skip to footer

Youtube Player Crashes (java.lang.nullpointerexception, Null Object Reference)

my YouTube Player crashes and I do not know why. This is my Java Code: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSt

Solution 1:

Right now we need some additional information to definitively answer this question, but it appears that this line is returning null, which suggests that you want to take a look at your resource XML and make sure you're grabbing the fragment properly.

getSupportFragmentManager().findFragmentById(R.id.youtube_player_fragment);

You may benefit from taking a look at this related question:

findFragmentById return null


I suspect that the problem may be that in your resources XML, you're not declaring the fragment using the support library.

Try using:

<fragment
  android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"

Solution 2:

I tried a lot of these suggestions but none worked for me. I finally ended up creating a container and then replacing it at runtime with the YouTubeFragment. This worked perfectly fine for me.

  1. Created a containerView in my layout file as, please note its just a part of my layout file

    <Viewandroid:padding="10dp"android:layout_width="match_parent"android:layout_height="1dp"android:background="@color/gray90"/><FrameLayoutandroid:padding="20dp"android:id="@+id/youtubeContent"android:layout_width="wrap_content"android:layout_height="wrap_content"/>
  2. Then I created an instance of the youtube fragment as below

    youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();

  3. I then replaced it using the standard way as below:-

    FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.youtubeContent,youTubePlayerFragment).commit();

I hope this helps others

Post a Comment for "Youtube Player Crashes (java.lang.nullpointerexception, Null Object Reference)"