NumberFormatException Error

前端 未结 5 1510
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 08:48

the code is:

editText2=(EditText) findViewById(R.id.editText2);
editText3=(EditText) findViewById(R.id.editText3);
float from_value= Float.parseFloat(editTe         


        
5条回答
  •  天涯浪人
    2020-12-11 09:03

    Seems like the String in editText2 is empty, so it fails to parse it as float.

    A possible solution is to check if the String is empty first, and then decide about default value, another is to catch the exception:

    float from_value;
    try {
        from_value = Float.parseFloat(editText2.getText().toString());
    }
    catch(NumberFormatException ex) {
        from_value = 0.0; // default ??
    }
    

提交回复
热议问题