简体   繁体   中英

Android NumberFormatException error with a simple number input from EditText

So I have a simple Edit-text with number input. User enters his phone number and it is saved into DB obviously that is the goal. now every time i do

int contactNumber=Integer.parseInt(mContactNumber.getText().toString());

I get an error thrown saying NumberFormatException for some reason it doesn't like a string of number. I have made sure that mContactNumber field in the android input field has the

android:input="number"

now i also tried just to be sure

int contactNumber=Integer.parseInt(mContactNumber.getText().toString().trim());

still the same error

Guys any ideas?

Try putting the Type to ' long ' instead of ' int '. Hope that will work for you.

This might be because you are leaving the text field empty. Are you sure you are not parsing an empty string?

You need to put a simple condition:

if(mContactNumber.getText().length()>0)
{
    int contactNumber=Integer.parseInt(mContactNumber.getText().toString().trim());
}

For phone Number you can not take it as Integer. try to take it as string only and after getting that string you can do the check for the numbers only by

TextUtils.isDigitsOnly(str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM