Solution 1 :
Kotlin doesn’t allow nullable types by default. The problem right here is that the fragment manager can be null, and so if it is the whole expression will be null, but you’re casting it to a non-nullable YoutubePlayerFragment
. The solution, simply, is to change the cast as YouTubePlayerFragment
to a nullable type by instead doing as YouTubePlayerFragment?
I’d also personally suggest that you check out the Kotlin Documentation on Null Safety
Problem :
Im implementing TouTube Api in my android Project. Im getting error in below line on “as” before YouTubePlayerFragment, “This cast can never succeed “. Im using to initialize youtube player in my fragment. I already tried android.enableJetifier=true in gradle.properties but its not helpfull
val youTubePlayerFragment = fragmentManager?.findFragmentById(R.id.vidVieW) as YouTubePlayerFragment
youTubePlayerFragment?.initialize(Config.getYoutubeApiKey(),monInitializedListener)
Here is monInitializedListener code. It works fine when i used this in activity
monInitializedListener = object: YouTubePlayer.OnInitializedListener {
override fun onInitializationSuccess(
provider: YouTubePlayer.Provider,
player: YouTubePlayer,
wasRestored: Boolean
) {
if (!wasRestored) {
mPlayer = player
}
// player.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL)
val mydef="BPGJUPcbQ58"
mPlayer?.loadVideo(mydef)
Toast.makeText(activity?.baseContext, "Fragment Visible", Toast.LENGTH_SHORT).show()
}
override fun onInitializationFailure(
p0: YouTubePlayer.Provider?,
p1: YouTubeInitializationResult?
) {
Toast.makeText(activity?.applicationContext, "onInitializationFailure()", Toast.LENGTH_LONG).show()
}
//
}
This is a fragment inside my XML
<fragment
android_name="com.google.android.youtube.player.YouTubePlayerFragment"
android_id="@+id/vidVieW"
android_layout_width="match_parent"
android_layout_height="200dp"
android_background="@drawable/orientation"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent"
/>
In logcat im getting this error
kotlin.TypeCastException: null cannot be cast to non-null type com.google.android.youtube.player.YouTubePlayerFragment
Comments
Comment posted by Bhavnik
Can you please add your monInitializedListener code ?
Comment posted by account
@Bhavnik kindly check it now
Comment posted by account
after using YouTubePlayerFragment? im not getting error anymore but issue is the video is also not playing
Comment posted by kyay10
@account That probably means that the youtubePlayerFragment is null. Make sure that the
Comment posted by account
Sir can you please elaborate your answer i didn’t get it
Comment posted by kyay10
the player fragment has not been added yet, which is why the video doesn’t play. Add the player fragment before you call
Comment posted by account
like this? val details = FragTwoNew() details.setArguments(getIntent(“BPGJUPcbQ58”).extras) var fragmentManager = fragmentManager var fragmentTransaction = fragmentManager?.beginTransaction(); fragmentTransaction?.add(R.id.vidView,details); fragmentTransaction?.commit();