Get integer value from edittext

别来无恙 提交于 2019-12-02 03:30:02

The problem seems that when Activity is bringed to front there is no value in EditText. Integer parser don't know how to parse empty String, so Exception is thrown.

You need to check for existing of text in that EditText before parsing

final int f = -1; // or other invalid value
if (from.getText().toString().length() > 0)
    f = Integer.parseInt(from.getText().toString());

Similarly for other EditText

as you can see from your Logcat:

08-28 19:34:44.790: E/AndroidRuntime(3346): Caused by: java.lang.NumberFormatException: Invalid int: ""

there was an empty input that couldn't be converted into an int.

try validating user input.

- Please check the value of EditText, is it a valid integer value or not.

Try out this code:

int i = 0;

try{

  i = Integer.parseInt(from.getText().toString());

}catch(NumberFormatException ex){

  System.out.println("Value at TextView is not a valid integer");

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