Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Cómo acceder a una función que está anidada dentro de un método de anulación
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() }

¿Cómo accedo a la función loadTrailer2 en el código principal? Load Trailer es la función dentro del objeto de YoutubePlayer.OnInitializedListener . Estoy tratando de acceder a él fuera de la función open trailer , también conocido como en el método onCreate

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Bueno, tal como está ahora, loadTrailer2 es una función local declarada dentro onInitializationSuccess : solo existe mientras se ejecuta esa función, por lo que no puede acceder a ella desde fuera de ese ámbito.

Puede moverlo al objeto en sí, pero dado que se basa en que se pasa el objeto YoutubePlayer , ¿cómo lo llamaría? ¿Tienes acceso a ese reproductor en onCreate ?

Si tuvo acceso a él (por ejemplo, cuando la inicialización tuvo éxito por primera vez, lo almacena en un ViewModel o algo así, y lo usa la próxima vez que cree un fragmento), todavía tiene un problema: el tipo de youtubePlayerInit es YouTubePlayer.OnInitializedListener , y está agregando otro método a su objeto, uno que no es parte de esa interfaz YouTubePlayer.OnInitializedListener .

Lo que eso significa es que nada sabe realmente que ese método existe. Si crea ese objeto anónimo dentro de una función, el compilador lo ve como un tipo especial que contiene ese método, por lo que puede acceder a él directamente:

 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

pero fuera de ese alcance, solo se ve como el tipo declarado al crear el object ( Thing en este ejemplo):

 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

Si quisieras acceder a esa función, tendrías que, por ejemplo, declarar una interfaz CoolFunctionHaver con esa función, y declarar myThing como ese tipo, o emitirlo si realmente tienes que hacerlo.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!