I'm trying to run code from Chapter 5 "Android Application Development All-in-One for Dummies".
The code in the book refers directly to view Id's in an onButtonClick function
fun onButtonClick(view: View){
...
if (checkBox.isChecked()){
...
}
but Android Studio highlights checkBox as an unresolved reference.
What I tried
I tried changing
fun onButtonClick(view: View){
...
val checkBox = view.findViewById<CheckBox>(R.id.checkBox)
if (checkBox.isChecked()){
...
}
but that crashed at runtime with checkBox is null. I looked at How can you add a reference to another view through attributes in android? and tried changing
fun onButtonClick(view: View){
...
val vp : ViewGroup = view.getParent()
val checkBox = vp.findViewById<CheckBox>(R.id.checkBox)
if (checkBox.isChecked()){
...
}
but that got type mismatch: inferred type is ViewParent (which doesn't have findViewByID)