I am getting Unresolved Reference when compiling
Error:(42, 26) Unresolved reference: r1 Error:(42, 36) Unresolved reference: ds
in the onClick method variables r1, and ds are shown to be in errors. in kotlin all varibales are final. so how come it is not accepting it. please advice following is the script
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val b1: Button = findViewById(R.id.add)
val a1: EditText = findViewById(R.id.opr1)
val a2: EditText = findViewById(R.id.opr2)
val d1: Int = (a1.getText().toString().toInt())
val d2: Int = (a2.getText().toString().toInt())
var r1: TextView = findViewById(R.id.res)
var ds :Int =d1+d2
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.add -> r1.text= (ds).toString()
}
}
}
d1 + d2only once at the beginning? You should probably move reading current values to the click listener. (Do what CrazyApple says.)