numberFormatException

烈酒焚心 提交于 2019-12-12 06:22:35

问题


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.



来源:https://stackoverflow.com/questions/5973493/numberformatexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!