0

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. 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

1 Answer 1

3

I have found it. The answer is that i have to make the MyListener class as inner So the correct declaration is

private inner class MyListener internal constructor(x: Int)...
Sign up to request clarification or add additional context in comments.

2 Comments

Otherwise if you don't want to use the inner you can also use [email protected]() as well.
Not true. A non-inner class has no associated outer class instance to reference. this@ syntax is only for resolving ambiguous scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.