Home > OS >  error in using toInt() function in android developement(Kotlin)
error in using toInt() function in android developement(Kotlin)

Time:01-06

I am getting error when i use toInt() function in android(Kotlin)

val num1=findViewById<EditText>(R.id.num1).text.toString()
val num2=findViewById<EditText>(R.id.num2).text.toString()

when i add toInt()

 val num1=findViewById<EditText>(R.id.num1).text.toString().toInt()
 val num2=findViewById<EditText>(R.id.num2).text.toString().toInt()

i am getting error when i run my app on my phone. I am a newbie in android developement so i decided to make a simple app which calculate two numbers but when i try to use toInt() my app not run on my phone

Note:when i remove toInt() my app smoothly run on my phone. [enter image description here][1]

screen shoot of error i am getting [1]: https://i.stack.imgur.com/Sl3cn.jpg

CodePudding user response:

Is it possibly crashing while attempting to cast empty field values as numbers?

Try using toIntOrNull and let me know what happens.

  •  Tags:  
  • Related