Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
How to access a function that's nested inside an override method
private fun openTrailer() {
    val trailerDialog = Dialog(this)
    val trailerDialogBinding = DialogTrailerBinding.inflate(layoutInflater)
    trailerDialog.setContentView(trailerDialogBinding.root)

    youtubePLayerInit = object : YouTubePlayer.OnInitializedListener {
        override fun onInitializationSuccess(
            p0: YouTubePlayer.Provider?,
            youtubePlayer: YouTubePlayer?,
            p2: Boolean
        ) {
            fun loadTrailer2(videoId: String) {
                youtubePlayer?.loadVideo(videoId)
            }
        }

        override fun onInitializationFailure(
            p0: YouTubePlayer.Provider?,
            p1: YouTubeInitializationResult?,
        ) {
            toast("la")
        }
    }
    if (!isInitialized) {
        trailerDialogBinding.vvMovieTrailer.initialize(youtubeApiKey, youtubePLayerInit)
        isInitialized = true
    } else {
        Log.e("initializerStatus", "Already Initialized")
    }
    trailerDialog.show()
}

How do I access access the loadTrailer2 function in the main code? Load Trailer is the function inside the object of the YoutubePlayer.OnInitializedListener. I'm trying to accesss it outside the open trailer function, aka in the onCreate method

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Well, as it stands now, loadTrailer2 is a local function declared inside onInitializationSuccess : it only exists while that function is running, so you can't access it from outside that scope.

You can move it to the object itself, but since it relies on the YoutubePlayer object being passed, what would you call it? Do you have access to that player in onCreate ?

If you had access to it (for example, when the initialization succeeded for the first time, you store it in a ViewModel or something, and use it the next time you create a fragment), you still have a problem: the type of youtubePlayerInit is YouTubePlayer.OnInitializedListener , and you're adding another method to your object, one that's not part of that YouTubePlayer.OnInitializedListener interface.

What that means is that nothing really knows that method exists. If you create that anonymous object inside a function, the compiler sees it as a special type that contains that method, so you can access it directly:

 interface Thing { fun onSomeEvent(code: Int) } fun main() { doSetup() } fun doSetup() { val myThing = object : Thing { override fun onSomeEvent(code: Int) { println("Event code: $code") } fun coolFunction() { println("Secret function accessed") } } // calling it inside the scope the object was declared in, so the function // is visible here myThing.coolFunction() } >> Secret function accessed

but outside of that scope, it just looks like the type declared when creating the object ( Thing in this example):

 interface Thing { fun onSomeEvent(code: Int) } fun main() { val myThing = doSetup() // this only knows it's a Thing, which doesn't have this method myThing.coolFunction() } fun doSetup(): Thing { val myThing = object : Thing { override fun onSomeEvent(code: Int) { println("Event code: $code") } fun coolFunction() { println("Secret function accessed") } } return myThing } >> Unresolved reference: coolFunction

If you wanted to access that function, you would have to, for example, declare a CoolFunctionHaver interface with that function, and declare myThing as that type, or cast it if you really had to.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda