Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda