Skip to content Skip to sidebar Skip to footer

Android:got Numberformatexxception While Getting Data From Edittext Field

java.lang.RuntimeException: Unable to start activity ComponentInfo { com.project/com.project.simple} : java.lang.NumberFormatException EditText et1,et2,et3; Button b1, b2; Float tw

Solution 1:

you are in onCreate() where the fields are specified. the user could not have time to enter any valid data. you need to move the fetching of the data somewhere else...like your onClick() perhaps.

Solution 2:

move your code from onCerate to an other method (for example the onClick method of the Button ) and you should try this:

try{
   two = Float.parseFloat(et1.getText().toString());
}catch(NumberFormatException e){
   two = 0;       
   Toast toast = Toast.makeText(this, 'Invalid number format on `two` field', 500);
   toast.show();  
} 

For each text fields what you want to read Comment: float format is 2.3 and don't use 2,3

Solution 3:

It seems that the input is not valid. Please check twice that you don't try to parse a letters to a number. If you have a float please check that you use the right locale for parsing. E.g. in germany is pi 3,1415... and not 3.1415...

If you cannot preparse the values you could put the parsing trys in try catch blocks like this:

float value;
try {
    value=Float.parseFloat(someString);
} catch(NumberFormatException e) {
    // input was no valid float
}

Post a Comment for "Android:got Numberformatexxception While Getting Data From Edittext Field"