I'm inside an anonymous inner class and wanting to reference the instance of the outside class. In Java it can be referenced using .this
preceded by the class name to resolve ambiguity. Like: MainActivity.this
. How can I achieve the same in Kotlin? Because the compiler is complaining 'expression 'this' cannot be a selector(occur after a dot)'
when I do that. Thanks!
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can use this@MainActivity
to reference the outer class instance.
Tip: I couldn't remember the syntax either, so I just wrote a simple example in Java and asked IntelliJ to convert the class to Kotlin to find the answer.