1

I am creating an Android Studio app in the Kotlin language and that utilizes the doAfterTextChanged function as a TextWatcher for an editText. However, when the user changes the text in the editText after it has already been changed, the app crashes. Is there any alternative (and simple) function I can use or something to add to my program. Here is an example of what I currently have:

editText.doAfterTextChanged() {
    score = score + 15 * editText.text.toString().toInt()
    totalscore.text = "Score: " + score.toString()
}
  • Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody Apr 26 '21 at 19:27
  • 1
    `the app crashes` - so you have to figure out why, then fix it – a_local_nobody Apr 26 '21 at 19:27

1 Answers1

1

What would happens when you clear the text from the EditText, e.g. making it empty (text = "")?

score = score + 15 * editText.text.toString().toInt()

In that scenario, you are basically trying to convert "" to an integer, which does not work obviously. NOTE, you are not seeing this the first time, because the code only triggers after text changed. Try this instead:

score += 15 * (editText.text.toString().toIntOrNull() ?: 0)
totalscore.text = "Score: $score"
Entreco
  • 12,738
  • 8
  • 75
  • 95