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

前端 未结 9 1218
时光取名叫无心
时光取名叫无心 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条回答
  •  萌比男神i
    2020-12-10 18:26

    you should wrap the Double.parseDouble.. statements in a try/catch clause, to catch any NumberFormatExceptions, and set other values where they fail

    edit:

    try{
        etKids = Double.parseDouble(noKids.getText().toString());
    } catch (final NumberFormatException e) {
        etKids = 1.0;
    }
    try{
        etGumballs = Double.parseDouble(noGumballs.getText().toString());
    } catch (final NumberFormatException e) {
        etGumballs = 1.0;
    } 
    

提交回复
热议问题