java.lang.NumberFormatException:无效的int:“”EXCEPTION

如果用户将edittext留空,则发生错误java.lang.NumberFormatException:Invalid int:“”。 错误来了

if (a=="" && b=="") 

也在线

  int result = Integer.parseInt(a) + Integer.parseInt(b); t1.setText(Integer.toString(result)); 

Calci.java

 package com.example.calculator; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Calci extends Activity { TextView t1; EditText e1, e2; Button add, sub, mul, div; Context c=this; String b, a; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calci); e1 = (EditText) findViewById(R.id.EditText01); e2 = (EditText) findViewById(R.id.EditText02); add = (Button) findViewById(R.id.add); sub = (Button) findViewById(R.id.sub); mul = (Button) findViewById(R.id.mul); div = (Button) findViewById(R.id.div); t1 = (TextView) findViewById(R.id.textView1); a = e1.getText().toString(); b = e2.getText().toString(); add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { if (a=="" && b==""){ AlertDialog.Builder a1 = new AlertDialog.Builder(c); // Setting Dialog Title a1.setTitle("Alert Dialog"); // Setting Dialog Message a1.setMessage("PLEASE ENTER SOMETHING"); a1.setPositiveButton("yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int button1) { // if this button is clicked, close // current activity dialog.cancel(); } }); // Showing Alert Message AlertDialog alertDialog = a1.create(); a1.show(); } else{ int result = Integer.parseInt(a) + Integer.parseInt(b); t1.setText(Integer.toString(result)); InputMethodManager imm = (InputMethodManager)getSystemService( Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(add.getWindowToken(), 0); } } }); } } 

logcat的:

 03-19 15:42:21.165: E/Trace(25381): error opening trace file: Permission denied (13) 03-19 15:42:21.165: D/ActivityThread(25381): setTargetHeapUtilization:0.25 03-19 15:42:21.165: D/ActivityThread(25381): setTargetHeapIdealFree:8388608 03-19 15:42:21.165: D/ActivityThread(25381): setTargetHeapConcurrentStart:2097152 03-19 15:42:21.385: D/libEGL(25381): loaded /system/lib/egl/libEGL_adreno200.so 03-19 15:42:21.465: D/libEGL(25381): loaded /system/lib/egl/libGLESv1_CM_adreno200.so 03-19 15:42:21.475: D/libEGL(25381): loaded /system/lib/egl/libGLESv2_adreno200.so 03-19 15:42:21.475: I/Adreno200-EGL(25381): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: (Merge) 03-19 15:42:21.475: I/Adreno200-EGL(25381): Build Date: 07/09/13 Tue 03-19 15:42:21.475: I/Adreno200-EGL(25381): Local Branch: AU_41 03-19 15:42:21.475: I/Adreno200-EGL(25381): Remote Branch: 03-19 15:42:21.475: I/Adreno200-EGL(25381): Local Patches: 03-19 15:42:21.475: I/Adreno200-EGL(25381): Reconstruct Branch: 03-19 15:42:21.675: D/OpenGLRenderer(25381): Enabling debug mode 0 03-19 15:42:24.325: D/AndroidRuntime(25381): Shutting down VM 03-19 15:42:24.325: W/dalvikvm(25381): threadid=1: thread exiting with uncaught exception (group=0x41972378) 03-19 15:42:24.395: E/AndroidRuntime(25381): FATAL EXCEPTION: main 03-19 15:42:24.395: E/AndroidRuntime(25381): java.lang.NumberFormatException: Invalid int: "" 03-19 15:42:24.395: E/AndroidRuntime(25381): at java.lang.Integer.invalidInt(Integer.java:138) 03-19 15:42:24.395: E/AndroidRuntime(25381): at java.lang.Integer.parseInt(Integer.java:359) 03-19 15:42:24.395: E/AndroidRuntime(25381): at java.lang.Integer.parseInt(Integer.java:332) 03-19 15:42:24.395: E/AndroidRuntime(25381): at com.example.calculator.Calci$1.onClick(Calci.java:67) 03-19 15:42:24.395: E/AndroidRuntime(25381): at android.view.View.performClick(View.java:4147) 03-19 15:42:24.395: E/AndroidRuntime(25381): at android.view.View$PerformClick.run(View.java:17161) 03-19 15:42:24.395: E/AndroidRuntime(25381): at android.os.Handler.handleCallback(Handler.java:615) 03-19 15:42:24.395: E/AndroidRuntime(25381): at android.os.Handler.dispatchMessage(Handler.java:92) 03-19 15:42:24.395: E/AndroidRuntime(25381): at android.os.Looper.loop(Looper.java:213) 03-19 15:42:24.395: E/AndroidRuntime(25381): at android.app.ActivityThread.main(ActivityThread.java:4787) 03-19 15:42:24.395: E/AndroidRuntime(25381): at java.lang.reflect.Method.invokeNative(Native Method) 03-19 15:42:24.395: E/AndroidRuntime(25381): at java.lang.reflect.Method.invoke(Method.java:511) 03-19 15:42:24.395: E/AndroidRuntime(25381): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 03-19 15:42:24.395: E/AndroidRuntime(25381): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 03-19 15:42:24.395: E/AndroidRuntime(25381): at dalvik.system.NativeStart.main(Native Method) 03-19 15:42:25.685: I/Process(25381): Sending signal. PID: 25381 SIG: 9 

任何人都有一个想法如何解决这些堆栈跟踪错误。

Solutions Collecting From Web of "java.lang.NumberFormatException:无效的int:“”EXCEPTION"

使用以下。

  a = e1.getText().toString().trim(); b = e2.getText().toString().trim(); if (a.equals("") && b.equals("") ){ } 

如果我们使用.equals() ,仍然有NumberFormatException机会,因为如果用户input一个空格或特殊的字符exception将会出现。 像这样改变你的代码

 @Override public void onClick(View arg0) { try{ a = e1.getText().toString(); b = e2.getText().toString(); int result = Integer.parseInt(a) + Integer.parseInt(b); t1.setText(Integer.toString(result)); InputMethodManager imm = (InputMethodManager)getSystemService( Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(add.getWindowToken(), 0); } catch(NumberFormatException e) { AlertDialog.Builder a1 = new AlertDialog.Builder(c); // Setting Dialog Title a1.setTitle("Alert Dialog"); // Setting Dialog Message a1.setMessage("Filed is Empty or Invalid Number"); a1.setPositiveButton("yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int button1) { // if this button is clicked, close // current activity dialog.cancel(); } }); // Showing Alert Message AlertDialog alertDialog = a1.create(); a1.show(); } } }); 
 if (a=="" && b=="") 

不要使用==来进行string比较。 请参阅如何比较Java中的string?

NumberFormatException的

由于您parsing用户提供的input,因此应该捕获exception并相应地处理parsing错误。

你正在将你的edittext的值转换成你的代码中的下面的string

 a = e1.getText().toString(); b = e2.getText().toString(); 

所以你必须使用

 a.equals("") && b.equals("") 

代替

 a=="" && b=="" 

还有一件事你必须改变,就是这一行

 AlertDialog.Builder a1 = new AlertDialog.Builder(c); 

 AlertDialog.Builder a1 = new AlertDialog.Builder(Calci.this); 

对于部分:

  int result = Integer.parseInt(a) + Integer.parseInt(b); t1.setText(Integer.toString(result)); 

尝试这个:

 int result = Integer.valueOf(a) + Integer.valueOf(b); t1.setText(Integer.toString(result)); 

这在一段时间以前帮了我一个非常类似的问题

请如下更改,

  add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { if (a.equals("") && b.equals("") ) { AlertDialog.Builder a1 = new AlertDialog.Builder(c); // Setting Dialog Title a1.setTitle("Alert Dialog"); // Setting Dialog Message a1.setMessage("PLEASE ENTER SOMETHING"); a1.setPositiveButton("yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int button1) { // if this button is clicked, close // current activity dialog.cancel(); } }); // Showing Alert Message AlertDialog alertDialog = a1.create(); a1.show(); } else{ int result = Integer.parseInt(a) + Integer.parseInt(b); t1.setText(""+result); InputMethodManager imm = (InputMethodManager)getSystemService( Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(add.getWindowToken(), 0); } } 

});

首先,你不应该使用==运算符比较string值,而应该使用String类中的equals()方法。

下一个改变。

 public void onClick(View arg0) { // First fetch the values of a & b here instead of onCreate() method a = e1.getText().toString().trim(); // trim() method will remove any white spaces b = e2.getText().toString().trim(); if (a.equals("") && b.equals("") ) { .... 

改变上面的事情,你的代码应该完美的工作。

这个问题是误导,它是关于整数的大小,因为java int是:2 ^ 32-1,您的整数可以是10位数字日志,例如:0123456789.然而,如果整数是11位数字,例如:0123456789 *** 1 ***。 那么你需要使用BigInteger,请参阅: https : //developer.android.com/reference/java/math/BigInteger.html