0

I simply use YoutubePlayer within one of my Tab Fragments, but couldn't integrate it!

With this code, it gives me this error.

Binary XML file line #0: Error inflating class com.google.android.youtube.player.YouTubePlayerView
                                                                       Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class com.google.android.youtube.player.YouTubePlayerView

Here is my Fragment class.

public class Frag_Trailer extends Fragment {

    YouTubePlayerView mYouTubePlayerView;
    YouTubePlayer.OnInitializedListener mOnInitializatedListener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView =  inflater.inflate(R.layout.fragment_trailer, container, false);
        mYouTubePlayerView = (YouTubePlayerView)rootView.findViewById(R.id.view_youtube);
        mYouTubePlayerView.initialize(YouTubeConfig.getYoutubeApiKey(), mOnInitializatedListener);

        mOnInitializatedListener = new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                youTubePlayer.loadVideo("_XgQA9Ab0Gw");
            }

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

        return rootView;
    }
}

Here are the dependencies that I have in build.gradle.

dependencies { 
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:cardview-v7:23.3.0' 
    implementation 'com.android.support:design:23.3.0' 
    implementation files('libs/YouTubeAndroidPlayerApi.jar') 
} 
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
juniordev
  • 49
  • 6
  • Please share your `build.gradle`. – Reaz Murshed Mar 04 '18 at 12:50
  • Should I add something to build.gradle? Here is it; dependencies { implementation 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.android.support:cardview-v7:23.3.0' implementation 'com.android.support:design:23.3.0' implementation files('libs/YouTubeAndroidPlayerApi.jar') } – juniordev Mar 04 '18 at 13:06
  • Please modify the jar inclusion in the `build.gradle` like this and let me know if the problem persists. `implementation fileTree(dir: 'libs', include: ['*.jar'])` – Reaz Murshed Mar 04 '18 at 13:17
  • Error still occurs. I think I made the class wrong. – juniordev Mar 04 '18 at 13:32

1 Answers1

0

based on this answer

you have to extend your activity with YouTubeBaseActivity. But in your case, you implement it on your Fragment, so change your view XML like this

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