Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] Kotlin: null cannot be cast to non-null type com.google.android.youtube.player.YouTubePlayerFragment

Posted on November 14, 2022

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 :

code pic

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

READ  [ANSWERED] gradle - Android AAR depending on AAR fails with javadoc generation
Powered by Inline Related Posts

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();

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress