I'm using androidx fragment but get always null reference of youTubeFragment and it's because of different casting.image
import androidx.fragment.app.Fragment
val youtubeFragment =
childFragmentManager.findFragmentById(R.id.playbackYoutubeFragment) as
com.google.android.youtube.player.YouTubePlayerFragment
youtube fragment import is:
import android.app.Fragment;
my layout:
<fragment
android:id="@+id/playbackYoutubeFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.youtube.player.
YouTubePlayerFragment"
app:layout_constraintTop_toTopOf="parent"/>
Is there any solution to solve that problem? or i have to change my fragment type.Actually i don't want to change my androidx fragment type. help is always appreciated.
It's because your childFragmentManager is from AndroidX package, therefore findFragmentById's return type is androidx.Fragment.
YouTubePlayerSupportFragment is android.support.v4.app.Fragment so in compile-time, these are two separate types that cannot be cast to each other.
The problem is Youtube SDK not using androidx. You can fix this by enabling jetifier, add following code to your gradle.properties:
android.enableJetifier=true
This will fix the problem by migrating all support classes in third party libraries to androidx, so your cast will succeed in runtime. However, Android studio warning will still be present - you can suppress it:
// CAST_NEVER_SUCCEEDS can be ignored - happens because Youtube SDK's fragment is not
// androidx.Fragment, but Jetifier will take care of that and cast will succeed
@Suppress("CAST_NEVER_SUCCEEDS")