Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

the code is:

editText2=(EditText) findViewById(R.id.editText2);
editText3=(EditText) findViewById(R.id.editText3);
float from_value= Float.parseFloat(editText2.getText().toString());
editText3.setText(" "+(from_value * 100.0));

And the logcat error is:

03-18 03:19:07.847: E/AndroidRuntime(875): Caused by: java.lang.NumberFormatException: Invalid float: ""

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
293 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 ??
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...