我正在製作一個應用程序,其中有三個editText和一個按鈕。我正在做的是在EditText中獲取一些輸入,進行計算並按按鈕進入另一個活動。點擊按鈕時的應用程序崩潰
我的問題是,當我不輸入任何值,然後按下按鈕的應用程序崩潰 這裏是我的代碼 -
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Double x = Double.parseDouble(editText1.getText().toString());
try{
if(x == null || x.equals("")){
Toast.makeText(getApplicationContext(), "Insert All The Fields", Toast.LENGTH_LONG).show();
return;
}else{
}
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "Insert All The Fields", Toast.LENGTH_LONG).show();
}
Double y = Double.parseDouble(editText2.getText().toString());
try{
if(y == null || y.equals("")){
Toast.makeText(getApplicationContext(), "Insert All The Fields", Toast.LENGTH_LONG).show();
return;
}else{
}
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "Insert All The Fields", Toast.LENGTH_LONG).show();
}
Double z = Double.parseDouble(editText3.getText().toString());
try{
if(z == null || z.equals("")){
Toast.makeText(getApplicationContext(), "Insert All The Fields", Toast.LENGTH_LONG).show();
return;
}else{
}
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "Insert All The Fields", Toast.LENGTH_LONG).show();
}
i = x+y+z;
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
intent.putExtra("key", +i);
startActivity(intent);
下面是logcat的
01-04 05:43:20.342: E/AndroidRuntime(1814): FATAL EXCEPTION: main
01-04 05:43:20.342: E/AndroidRuntime(1814): Process: com.example.calculatorpd, PID: 1814
01-04 05:43:20.342: E/AndroidRuntime(1814): java.lang.NumberFormatException: Invalid double: ""
01-04 05:43:20.342: E/AndroidRuntime(1814): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
01-04 05:43:20.342: E/AndroidRuntime(1814): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
01-04 05:43:20.342: E/AndroidRuntime(1814): at java.lang.Double.parseDouble(Double.java:295)
01-04 05:43:20.342: E/AndroidRuntime(1814): at com.example.calculatorpd.MainActivity$1.onClick(MainActivity.java:76)
01-04 05:43:20.342: E/AndroidRuntime(1814): at android.view.View.performClick(View.java:4438)
01-04 05:43:20.342: E/AndroidRuntime(1814): at android.view.View$PerformClick.run(View.java:18422)
01-04 05:43:20.342: E/AndroidRuntime(1814): at android.os.Handler.handleCallback(Handler.java:733)
01-04 05:43:20.342: E/AndroidRuntime(1814): at android.os.Handler.dispatchMessage(Handler.java:95)
01-04 05:43:20.342: E/AndroidRuntime(1814): at android.os.Looper.loop(Looper.java:136)
01-04 05:43:20.342: E/AndroidRuntime(1814): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-04 05:43:20.342: E/AndroidRuntime(1814): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 05:43:20.342: E/AndroidRuntime(1814): at java.lang.reflect.Method.invoke(Method.java:515)
01-04 05:43:20.342: E/AndroidRuntime(1814): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-04 05:43:20.342: E/AndroidRuntime(1814): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-04 05:43:20.342: E/AndroidRuntime(1814): at dalvik.system.NativeStart.main(Native Method)
把你的logcat輸出 – 2015-01-04 10:38:17
'x.equals(「」)'? 'x'是雙倍的,它如何可以是''「'?你必須爲null – 2015-01-04 10:40:52