Get integer value from edittext

前端 未结 3 369

When I try to run the code below, my program has stops. I want this code to get the value from edittext but it´s not working as I expected it. What am I doing wrong

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-25 00:47

    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

提交回复
热议问题