基本上,我有一个edittext框,通过xml限制为10个数字。当我输入少于7的东西时,它会显示一个祝酒词。 7个整数保存字符串。 8或9个整数显示其适当的吐司。但是,当我输入完整的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();
}
}
我已经尝试了所有我能想到的东西而没有解决它。我确定这只是我忽略的事情。
答案 0 :(得分:3)
当您输入应用程序崩溃的任何内容时,java中int的最大值为2,147,483,647。尝试使用最大值为9,223,372,036,854,775,807的long。
此外,如果您要保存电话号码,我建议您使用字符串,除非您需要对电话号码进行数学计算(不太可能)。某些电话号码包含&#39; +&#39;,&#39; *&#39;,&#39;#&#39;等字符。
答案 1 :(得分:0)
使用double而不是int值