gpt4 book ai didi

java - 尝试保存 10 位数字电话号码,但抛出无效 int 错误

转载 作者:行者123 更新时间:2023-12-01 18:26:14 25 4
gpt4 key购买 nike

基本上,我有一个编辑文本框,通过 xml 限制为 10 个数字。当我输入任何小于 7 的内容时,它会显示 toast 。 7 个整数按应有的方式保存字符串。 8 或 9 整数表示其合适的 toast。但是,当我输入完整的 10 位数字时,我收到此错误...

09-28 15:29:11.312: E/AndroidRuntime(19193): java.lang.IllegalStateException: Could notexecute method of the activity
09-28 15:29:11.312: E/AndroidRuntime(19193): at android.view.View$1.onClick(View.java:3830)
09-28 15:29:11.312: E/AndroidRuntime(19193): at android.view.View.performClick(View.java:4445)
09-28 15:29:11.312: E/AndroidRuntime(19193): at android.view.View$PerformClick.run(View.java:18446)
09-28 15:29:11.312: E/AndroidRuntime(19193): at android.os.Handler.handleCallback(Handler.java:733)
09-28 15:29:11.312: E/AndroidRuntime(19193): at android.os.Handler.dispatchMessage(Handler.java:95)
09-28 15:29:11.312: E/AndroidRuntime(19193): at android.os.Looper.loop(Looper.java:136)
09-28 15:29:11.312: E/AndroidRuntime(19193): at android.app.ActivityThread.main(ActivityThread.java:5140)
09-28 15:29:11.312: E/AndroidRuntime(19193): at java.lang.reflect.Method.invoke(Native Method)
09-28 15:29:11.312: E/AndroidRuntime(19193): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
09-28 15:29:11.312: E/AndroidRuntime(19193): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
09-28 15:29:11.312: E/AndroidRuntime(19193): Caused by: java.lang.reflect.InvocationTargetException
09-28 15:29:11.312: E/AndroidRuntime(19193): at java.lang.reflect.Method.invoke(Native Method)
09-28 15:29:11.312: E/AndroidRuntime(19193): at android.view.View$1.onClick(View.java:3825)
09-28 15:29:11.312: E/AndroidRuntime(19193): ... 9 more
09-28 15:29:11.312: E/AndroidRuntime(19193): Caused by: java.lang.NumberFormatException: Invalid int: "3557565542"
09-28 15:29:11.312: E/AndroidRuntime(19193): at java.lang.Integer.invalidInt(Integer.java:137)
09-28 15:29:11.312: E/AndroidRuntime(19193): at java.lang.Integer.parse(Integer.java:377)
09-28 15:29:11.312: E/AndroidRuntime(19193): at java.lang.Integer.parseInt(Integer.java:365)
09-28 15:29:11.312: E/AndroidRuntime(19193): at java.lang.Integer.parseInt(Integer.java:331)

以及方法...

 public void onOk(View src) {
Product p = new Product();
p.name = _name.getText().toString();
p.number = Integer.parseInt(number.getText().toString());

if ((number.getText().toString().trim().length() == 7)) {
Toast.makeText(getApplicationContext(), "Please enter the full 10 digit phone number for best results", Toast.LENGTH_LONG).show();
Intent result = new Intent();
result.putExtra("product", p);
setResult(RESULT_OK, result);
finish();
}
else if (number.getText().toString().trim().length() < 7) {
Toast.makeText(getApplicationContext(), "Please enter at least 7 digits", Toast.LENGTH_LONG).show();
}
else if ((number.getText().toString().trim().length() == 8) || number.getText().toString().trim().length() == 9)) {
Toast.makeText(getApplicationContext(), "You entered " + _price.getText().toString().trim().length() + ". Please enter a 10 digit phone number.", Toast.LENGTH_LONG).show();
} else {
Intent result = new Intent();
result.putExtra("product", p);
setResult(RESULT_OK, result);
finish();
}
}

我已经尝试了所有我能想到的方法,但没有解决问题。我确信这只是我忽略的事情。

最佳答案

java 中 int 的最大值是 2,147,483,647,当您输入任何高于该值的值时,您的应用程序就会崩溃。尝试使用最大值为 9,223,372,036,854,775,807 的 long。

此外,如果您要保存电话号码,我建议您使用字符串,除非您需要对电话号码进行数学运算(不太可能)。有些电话号码包含“+”、“*”、“#”等字符。

关于java - 尝试保存 10 位数字电话号码,但抛出无效 int 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26089433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com