Getting a NumberFormatException from a numerical EditText field

前端 未结 7 820
长情又很酷
长情又很酷 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

    I think initial Edit text will be empty so "" (blank is not a valid input) is thowing error you should fill the data in Edit text and then on click on any event you should do the parsing work....

    0 讨论(0)
  • 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....
    }
    
    0 讨论(0)
  • 2021-01-25 12:50

    Edit your layout file

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
    android:text="0"
        android:id="@+id/num_sensors" /> 
    
    0 讨论(0)
  • 2021-01-25 12:50

    You are getting this error because there is nothing or a ""/Empty String initially in your edit text. It makes no sense to get the text of an edit text when it is still being display. You may probably want to have a button or some other event which will signify that the user is done with inputting the value in the edit text and then you can go ahead and get the value of the edit text. But always have a check for the Empty String

    0 讨论(0)
  • 2021-01-25 13:09

    If you try to parse empty string it will always throw NumberFormatException.

    Change your code to following.

    int i;
    String change = numSensors.getText().toString();
    if(change.length>0)
    {
      i = Integer.parseInt(change);
    }
    else
    {
       i=0;
    }  
    
    0 讨论(0)
  • 2021-01-25 13:13

    Your EditText initially has an empty String for its value (ie ""). This is obviously not a number, so when you try to call int i = Integer.parseInt(change); it is giving you an error saying as such.

    There are a few ways to fix this, but it basically boils down to either prevent the error by setting an initial value, or detecting an empty string and handling it properly.

    To prevent the error from occurring...

    EditText numSensors = (EditText) findViewById(R.id.num_sensors);
    String change = numSensors.getText().toString();
    if (change.equals("")){ // detect an empty string and set it to "0" instead
        change = "0";
    }
    int i = Integer.parseInt(change);
    

    Or set the initial value as "0" for the EditText, however this also displays the value 0 in the EditText on your interface rather than being empty, so it might not be suitable for all purposes...

    <EditText android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:inputType="number"
              android:text="0"
              android:id="@+id/num_sensors" />
    

    If you want to detect the error and handle it properly, you would do this...

    EditText numSensors = (EditText) findViewById(R.id.num_sensors);
    String change = numSensors.getText().toString();
    int i = 0; // set it to 0 as the default
    try {
        i = Integer.parseInt(change);
    }
    catch (NumberFormatException e){}
    

    This basically sets the value of i = 0, and will only change it to a different value if the try{} code doesn't give an error.

    0 讨论(0)
提交回复
热议问题