[英]Kotlin inner class accessing outer class?
How do I call a method in outer class from the inner class? 如何从内部类调用外部类中的方法? I can pass it as context, but I can't call methods on it 我可以将其作为上下文传递,但不能在其上调用方法
loginButton.setOnClickListener {
ssoManager.login(emailEditText.text.toString(), passwordEditText.text.toString())
.subscribe(object: Consumer<SSOToken> {
val intent = Intent(this@LoginActiviy, PasscodeActivity::class.java)
this@LoginActiviy.startActivity(intent)
})
I'm not sure what APIs you're using here, I'm gonna assume that your Consumer
is java.util.function.Consumer
for the sake of the answer. 我不确定您在这里使用的是什么API,为了得到答案,我将假设您的Consumer
是java.util.function.Consumer
。
You are writing code directly in the body of your object
, and not inside a function. 您正在直接在object
主体中而不是在函数内部编写代码。 The first line of creating the Intent
only works because you're declaring a property (and not a local variable!). 创建Intent
的第一行仅适用于您声明属性(而不是局部变量!)的情况。
What you should do instead is implement the appropriate methods of Consumer
, and write the code you want to execute inside there: 您应该执行的是实现Consumer
的适当方法,并在其中编写要执行的代码:
loginButton.setOnClickListener {
ssoManager.login()
.subscribe(
object : Consumer<SSOToken> {
val foo = "bar" // this is a property of the object
override fun accept(t: SSOToken) {
val intent = Intent(this@LoginActiviy, PasscodeActivity::class.java)
this@LoginActiviy.startActivity(intent)
}
}
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.