How to parse a double from EditText to TextView? (Android)

前端 未结 9 1254
时光取名叫无心
时光取名叫无心 2020-12-10 18:10

I\'m realy beginning to learn Java. When I run this code everything works fine till I leave my EditText boxes in the from empty and hit the run button. Then I get:

9条回答
  •  一个人的身影
    2020-12-10 18:27

    This is a java question, not an android question.

    You should handle the NumberFormat exception in your code. What happens if somebody enters "abc" into the text box? What do you want your app to do? You handle exceptions using try/catch blocks: http://tutorials.jenkov.com/java-exception-handling/basic-try-catch-finally.html

    You might also want to check if noKids.getText().toString() is empty before trying to convert it. It might make sense for there to be a different feedback to the user if the string is "" vs if the string is "abc".

提交回复
热议问题