Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Youtube player support fragment no longer working on Android studio 3.2 (androidx)

Tags:

I just updated my Android Studio to version 3.2 and followed instructions to use androidx.

I've been using a Youtube fragment inside a Fragment activity and everything worked perfectly but, after the update, these 3 simple lines now give me the error "Cannot resolve method 'add(...)'":

YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance(); FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit(); 

...and when i try to use "replace" instead of "add" it says: "Wrong 2nd argument type. Found: 'com.google.android.youtube.player.YouTubePlayerSupportFragment', required: 'androidx.fragment.app.Fragment'"

...which makes me think that the problem has to do with the new AndroidX feature.

The problem is that the add method wants the second parameter of type:

androidx.fragment.app.Fragment 

...but the YouTubePlayerSupportFragment returns a:

android.support.v4.app.Fragment 

Does anyone know how to solve this problem? Is there a way to cast the "android.support.v4.app.Fragment" into the "androidx.fragment.app.Fragment"?

like image 246
Nicola Salvaro Avatar asked Sep 30 '18 10:09

Nicola Salvaro


2 Answers

Just use transaction.replace. Ignore the error, it'll work. Google hasn't refactored youtube api library to androidx yet.

like image 79
Bek Avatar answered Sep 19 '22 15:09

Bek


Just copy the original java file (com.google.android.youtube.player.YouTubePlayerFragment) to your project to the same package but different class name etc. com.google.android.youtube.player.YouTubePlayerFragmentX, and update the extends class from android.app.Fragment to androidx.fragment.app.Fragment.

The implementation is the same:

YouTubePlayerFragmentX youTubePlayerFragment = YouTubePlayerFragmentX.newInstance(); FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit(); 

Tested... it's working.

like image 43
Hosszuful Avatar answered Sep 21 '22 15:09

Hosszuful