Getting a NumberFormatException from a numerical EditText field

前端 未结 7 899
长情又很酷
长情又很酷 2021-01-25 12:16

I\'m trying to create a for-loop to add views to a layout. The for-loop is working fine (I tried it with initialized variables) but I need to get an in

7条回答
  •  甜味超标
    2021-01-25 12:48

    bez you are passing empty String in parseInt() so check before parsing string to int as:

    EditText numSensors  = (EditText) findViewById(R.id.num_sensors);
    String change  = numSensors.getText().toString();
    if (change.trim().equals("")) {
     //DO SOMETHING HERE
    }
    else
    {
    int i = Integer.parseInt(change);
    YOUR CODE HERE....
    }
    

提交回复
热议问题