从 EditText 输入简单数字时出现 Android NumberFormatException 错误
所以我有一个带有数字输入的简单编辑文本。 用户输入他的电话号码,并将其保存到数据库中,显然这就是目标。 现在每次我这样做时
int contactNumber=Integer.parseInt(mContactNumber.getText().toString());
都会抛出一个错误,说 NumberFormatException 由于某种原因它不喜欢数字字符串。我已经确保 android 输入字段中的 mContactNumber 字段有现在
android:input="number"
我也尝试只是为了确保
int contactNumber=Integer.parseInt(mContactNumber.getText().toString().trim());
仍然是相同的错误
伙计们有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试将类型设置为“long”而不是“int”。希望这对你有用。
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:
对于电话号码,您不能将其视为整数。尝试仅将其视为字符串,在获取该字符串后,您只能通过以下方式检查数字
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