简体   繁体   English

嵌套 class Kotlin 内未解析的引用

[英]Unresolved reference inside a nested class Kotlin

I have an activity with a variable controller and an nested class A. Inside the A class i want to have access to the controller variable.我有一个带有变量 controller 和嵌套 class A 的活动。在 A class 内部我想访问 Z590318ABAZ03F2C0E1 变量。 Hoe can i do this?我可以这样做吗?

class MyActivity: AppCompatActivity() {

   private val controller: MyController

   ....methods of activity...

   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        controller = Controller()
        // Doing stuff
   }


   private class MyListener internal constructor(x: Int): LocalClass.Listener {

      override fun onCallOne() {
           // I get an unresolved reference here
           controller.method()
      }

      override fun onCallTwo() {}

      override fun onCallThree() {}

   }

}

The above code is made out of AndroidStudio autoconversion when i pasted the same from Java code, where the controller variable is accessible from the nested class MyListener当我从 Java 代码粘贴相同的代码时,上面的代码是由 AndroidStudio 自动转换制成的,其中 controller 变量可从嵌套的 class MyListener 访问

I have found it.我找到了。 The answer is that i have to make the MyListener class as inner So the correct declaration is答案是我必须将 MyListener class 作为内部所以正确的声明是

private inner class MyListener internal constructor(x: Int)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM