简体   繁体   English

Android NumberFormatException错误,带有从EditText输入的简单数字

[英]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. 由于某种原因,我收到一个错误消息,说NumberFormatException,它不喜欢数字字符串。 I have made sure that mContactNumber field in the android input field has the 我确保android输入字段中的mContactNumber字段具有

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 '. 尝试将Type设置为long而不是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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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