numberFormatException

2019-09-09 12:06发布

问题:

Is this sequence correct in android? I am tring to get EditText value and convert it into integer.

startTime_hour_int=Integer.parseInt(startTime_hour_edittext.getEditableText().toString());

Because my logcat throws the below error. 05-12 10:26:35.536: ERROR/AndroidRuntime(293): java.lang.NumberFormatException: unable to parse '' as integer

Can anyone tell me, how to solve this?

回答1:

@Andro_Selva

If textbox startTime_hour_edittext is blank then Integer.parseInt is trying to parse "" into integer thats why you are getting NumberFormatException

so before use of startTime_hour_int=Integer.parseInt(startTime_hour_edittext.getEditableText().toString());

check condition

if(!startTime_hour_edittext.getText().toString().equalsIgnoreCase("")) {
startTime_hour_int=Integer.parseInt(startTime_hour_edittext.getEditableText().toString());
}


回答2:

Do you call this code from some kind of listener on the EditBox? This can happen when you delete all content from the box. Just check that the text is not empty before parsing it to int.