Android Studio Variables not appearing in OnClickListener body

て烟熏妆下的殇ゞ 提交于 2019-12-24 21:17:17

问题


I'm working in android studio using the debugger.

var loginButton = findViewById<Button>(R.id.loginButtonFinal)
var emailInput = findViewById<EditText>(R.id.emailInput)
var e = 2
loginButton.setOnClickListener {
    println("Log In Button pressed, will log in now")
    //            insert code for login in here
    //            signIn(email = emailInput.)
}

If I set a breakpoint in the debugger at the line containing var e = 2 I will see loginButton and emailInput output to the variable section of the debugger. But if I place the debugger inside of loginButton.setOnClickListener they no longer appear.

I would like to be able to see variables after the click has occurred. What can I do?


回答1:


Within the click listener, those variables are out of scope and the new scope of execution becomes the anonymous class.

If you would like to still inspect the views, then you should declare them as fields within the Activity class.



来源:https://stackoverflow.com/questions/58105131/android-studio-variables-not-appearing-in-onclicklistener-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!