2015-01-04 88 views
-2

我正在製作一個應用程序,其中有三個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) 
+2

把你的logcat輸出 – 2015-01-04 10:38:17

+2

'x.equals(「」)'? 'x'是雙倍的,它如何可以是''「'?你必須爲null – 2015-01-04 10:40:52

回答

1

問題是你是解析一些空的文本。 因此,在解析導致異常的double之前,請首先檢查文本本身。

 btn.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      Double x, y,z; 

      try 
      { 
      String a = editText1.getText().toString(); 

      if(a == null || a.equals("")) 
      { 
       x = 0; 
       Toast.makeText(getApplicationContext(), 
       "Insert All The Fields", Toast.LENGTH_LONG).show(); 
        return; 
      } 
      else 
      { 
       x = Double.parseDouble(a); 
      } 
      } 
       catch(Exception e){ 
        Toast.makeText(getApplicationContext(), 
        "Insert All The Fields", Toast.LENGTH_LONG).show(); 
       } 



      try{ 

       String b = editText2.getText().toString(); 

       if(b == null || b.equals("")) 
      { 
        y = 0; 
        Toast.makeText(getApplicationContext(), 
        "Insert All The Fields", Toast.LENGTH_LONG).show(); 
         return; 
       }else{ 
       y = Double.parseDouble(editText2.getText().toString()); 
       } 
       } 
        catch(Exception e){ 
         Toast.makeText(getApplicationContext(), 
         "Insert All The Fields", Toast.LENGTH_LONG).show(); 
        } 




      try{ 
       String c = editText3.getText().toString(); 

       if(c == null || c.equals("")) 
      { 

        z = 0; 
        Toast.makeText(getApplicationContext(), 
        "Insert All The Fields", Toast.LENGTH_LONG).show(); 
         return; 
       }else{ 
        z = Double.parseDouble(editText3.getText().toString()); 
       } 
       } 
        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); 
+0

'x.equals(「」)'是這樣嗎? – 2015-01-04 10:48:09

+0

當我添加x = 0時出現錯誤,「無法從int轉換爲雙倍」 – Karma5 2015-01-04 10:57:12

+0

@shayanpourvatan - 再次修改整個代碼。感謝通知.. :) – 2015-01-04 10:57:56

0

您使用此行

Double x = Double.parseDouble(editText1.getText().toString()); 

你需要檢查,如果該字符串代表一個數字前。

if(editText1.getText().toString() == null || editText1.getText().toString().equals("")){ 

     Toast.makeText(getApplicationContext(), 
     "Insert All The Fields", Toast.LENGTH_LONG).show(); 
      return; 
    }else{ 
      Double x = Double.parseDouble(editText1.getText().toString()); 
    } 

你需要添加更多的測試來驗證是沒有別的但一些

相關問題