0

My app has a kind of jukebox for Youtube videos. I use a YouTubePlayerFragment and my layout looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <ListView
        android:id="@+id/lv_songs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <fragment
        android:id="@+id/youtube_fragment"
        android:name="com.google.android.youtube.player.YouTubePlayerFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

This is my code:

void init() {
     youtubePlayer = (YouTubePlayerFragment) getActivity().getFragmentManager().findFragmentById(R.id.youtube_fragment);
     youtubePlayer.initialize(AppUtils.API_KEY, this);
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer ytplayer,

        boolean wasRestored) {
           player = ytplayer;
        }

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

        @Override
        public void onResume() {
            super.onResume();
            ((HomeActivity_) getActivity()).getSupportActionBar().setTitle(getString(R.string.jukebox));
        }
}

When I open this view for the first time, there is no problem. I can choose a video from my ListView and play it. But when I come back to this view again, I get a InflateException:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #12: Duplicate id 0x7f09004a, tag null, or parent id 0xffffffff with another fragment for com.google.android.youtube.player.YouTubePlayerFragment

So I guess that the YouTubePlayerFragment is not recycled onPause of my fragment. Even when I call

getActivity().getSupportFragmentManager().beginTransaction().remove(getActivity().getSupportFragmentManager().findFragmentById(R.id.youtube_fragment)).commit();

in onPause, there is still the same error. How can I avoid this Fragment to get inflated for a second time, or delete the inflated Fragment each time I leave this view?

JensJensen
  • 1,017
  • 1
  • 12
  • 25

2 Answers2

1

Since you're using the SupportFragmentManager you should also be using the support youtube fragment:

<fragment
    android:id="@+id/youtube_fragment"
    android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
Simas
  • 43,548
  • 10
  • 88
  • 116
  • When I change it to com.google.android.youtube.player.YouTubePlayerSupportFragment, I get a NPE when trying to open the View for the first time... – JensJensen Jan 11 '15 at 23:37
  • @JensJensen it's unclear what you're using. In 1 place you call `getFragmentManager` in other `getSupportFragmentManager`. Stick to one of them based on your `minSdk`. – Simas Jan 11 '15 at 23:39
  • I changed everything to 'SupportFragmentManager'. But as I said, I get an NPW when trying to call youtubePlayer.initialize(). But I definitely call youtubePlayer = (YouTubePlayerSupportFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.youtube_fragment); before. – JensJensen Jan 11 '15 at 23:42
1

I had same problem. For some reason, the YouTubePlayerFragment kept crashing my activity when opening it for the second time - also with the "InflateException" error message. In my case, the YouTubePlayerFragment is located inside another fragment. Originally, I used to decalre it within the XML file.

Instead of declaring the YouTubePlayerFragment inside the XML file, I switched to instantiating and inflating it inside my Java code. This solved the problem.