2012-05-27 54 views
1

我正在創建一個應用程序,它將一個數字除以另一個數字。我試圖添加代碼來顯示一個對話框,當有一個空的編輯文本,但應用程序關閉。這是我的代碼。Android初學者:空編輯文本

public class KdCalculator extends Activity implements OnClickListener{ 

    EditText kills; 
    EditText deaths; 
    TextView text; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.kd_activity); 

     Button calculate = (Button)findViewById(R.id.ButtonCalculate); 
      calculate.setOnClickListener(this); 

     kills = (EditText)findViewById(R.id.EditTextKills); 
     deaths = (EditText)findViewById(R.id.EditTextDeaths); 

    } 

    public void onClick(View paramView) 
     { 
      boolean invalid = false; 

      String str1 = kills.getText().toString(); 
      String str2 = deaths.getText().toString(); 

      if (str1.equals("") || str2.equals("")) 
      { 
       new AlertDialog.Builder(this) 
       .setTitle("No Number") 
       .setMessage("Please enter a number for both kills and deaths!") 
       .setNeutralButton("Ok", null) 
       .show(); 
       invalid = true; 
      } 

      double d1 = Double.parseDouble(str2); 
      double d2 = Double.parseDouble(str1); 

      if ((d1 == 0.0D) || (d2 == 0.0D)) 
      { 
       new AlertDialog.Builder(this) 
      .setTitle("Invalid Number") 
      .setMessage("Please enter a number different than 0 for both kills and deaths!") 
      .setNeutralButton("Ok", null) 
      .show(); 
       invalid = true; 
      } 
      double d3 = d2/d1; 
      DecimalFormat localDecimalFormat = new DecimalFormat("#.###"); 
      String str3 = "Your K/D Ratio is : " + localDecimalFormat.format(d3); 

      if(!invalid){ 
      new AlertDialog.Builder(this) 
      .setTitle("K/D Ratio") 
      .setMessage(str3) 
      .setNeutralButton("Ok", null) 
      .show(); 
      } 
      invalid = false; 
     } 


} 

的logcat的錯誤是:

05-27 11:43:53.327: W/dalvikvm(6090): threadid=1: thread exiting with uncaught exception (group=0x40020b80) 
05-27 11:43:53.357: E/AndroidRuntime(6090): FATAL EXCEPTION: main 
05-27 11:43:53.357: E/AndroidRuntime(6090): java.lang.NumberFormatException: 
05-27 11:43:53.357: E/AndroidRuntime(6090):  at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267) 
05-27 11:43:53.357: E/AndroidRuntime(6090):  at java.lang.Double.parseDouble(Double.java:287) 
05-27 11:43:53.357: E/AndroidRuntime(6090):  at com.blackops2.KdCalculator.onClick(KdCalculator.java:53) 
05-27 11:43:53.357: E/AndroidRuntime(6090):  at android.view.View.performClick(View.java:2411) 
05-27 11:43:53.357: E/AndroidRuntime(6090):  at android.view.View$PerformClick.run(View.java:8819) 
05-27 11:43:53.357: E/AndroidRuntime(6090):  at android.os.Handler.handleCallback(Handler.java:587) 
05-27 11:43:53.357: E/AndroidRuntime(6090):  at android.os.Handler.dispatchMessage(Handler.java:92) 
05-27 11:43:53.357: E/AndroidRuntime(6090):  at android.os.Looper.loop(Looper.java:123) 
05-27 11:43:53.357: E/AndroidRuntime(6090):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
05-27 11:43:53.357: E/AndroidRuntime(6090):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-27 11:43:53.357: E/AndroidRuntime(6090):  at java.lang.reflect.Method.invoke(Method.java:521) 
05-27 11:43:53.357: E/AndroidRuntime(6090):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
05-27 11:43:53.357: E/AndroidRuntime(6090):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
05-27 11:43:53.357: E/AndroidRuntime(6090):  at dalvik.system.NativeStart.main(Native Method) 

回答

2

即使你EditText是空的你還在分析從這些EditText爲你的代碼空Strings繼續顯示您AlertDialog後執行,而應該從onClick方法返回停止任何計算(當EditText爲空或不填寫):

public void onClick(View paramView) { 
      String str1 = kills.getText().toString(); 
      String str2 = deaths.getText().toString(); 

      if (str1.equals("") || str2.equals("")) { 
       new AlertDialog.Builder(this) 
       .setTitle("No Number") 
       .setMessage("Please enter a number for both kills and deaths!") 
       .setNeutralButton("Ok", null) 
       .show(); 
       return; 
      } 
      double d1 = Double.parseDouble(str2); 
      double d2 = Double.parseDouble(str1); 
      if ((d1 == 0.0D) || (d2 == 0.0D)) { 
       new AlertDialog.Builder(this) 
      .setTitle("Invalid Number") 
      .setMessage("Please enter a number different than 0 for both kills and deaths!") 
      .setNeutralButton("Ok", null) 
      .show(); 
      return; 
      } 
      double d3 = d2/d1; 
      DecimalFormat localDecimalFormat = new DecimalFormat("#.###"); 
      String str3 = "Your K/D Ratio is : " + localDecimalFormat.format(d3); 

      new AlertDialog.Builder(this) 
      .setTitle("K/D Ratio") 
      .setMessage(str3) 
      .setNeutralButton("Ok", null) 
      .show(); 
     } 
+0

完美的答案,+1 – Lucifer

+0

謝謝你的幫助! – drew

2

嘗試打電話Double.parseDouble(String)之前記錄的str1 & str2值;你可以使用Log.d(String, String)來做到這一點。

根據堆棧跟蹤,其中一個值不能轉換爲數字。

我通常會做的是將任何數字格式化調用放到try...catch塊中 - 這種操作可能會產生很多錯誤。這可能是避免此代碼中進一步崩潰的最成功的方法。

0

嘗試......

DecimalFormat localDecimalFormat = new DecimalFormat("0.000"); 

而只提嘗試檢查海峽荷蘭國際集團str1和str2的,並且要謹慎..雙不零錯誤給分

0

我不知道,但嘗試這樣的產品......

public class KdCalculator extends Activity implements OnClickListener 
{  
    double d1 = 0.0; 
    double d2 = 0.0;  
    EditText kills; 
    EditText deaths; 
    TextView text; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.kd_activity); 

     Button calculate = (Button)findViewById(R.id.ButtonCalculate); 
      calculate.setOnClickListener(this); 

     kills = (EditText)findViewById(R.id.EditTextKills); 
     deaths = (EditText)findViewById(R.id.EditTextDeaths); 

    } 

    public void onClick(View paramView) 
     { 
      boolean invalid = false; 

      String str1 = kills.getText().toString(); 
      String str2 = deaths.getText().toString(); 

      if (str1.equals("") || str2.equals("")) 
      { 
       new AlertDialog.Builder(this) 
       .setTitle("No Number") 
       .setMessage("Please enter a number for both kills and deaths!") 
       .setNeutralButton("Ok", null) 
       .show(); 
       invalid = true; 
      } 

      d1 = Double.parseDouble(str2); 
      d2 = Double.parseDouble(str1); 

      if ((d1 == 0.0D) || (d2 == 0.0D)) 
      { 
       new AlertDialog.Builder(this) 
      .setTitle("Invalid Number") 
      .setMessage("Please enter a number different than 0 for both kills and eaths!") 
      .setNeutralButton("Ok", null) 
      .show(); 
       invalid = true; 
      } 
      double d3 = d2/d1; 
      DecimalFormat localDecimalFormat = new DecimalFormat("#.###"); 
      String str3 = "Your K/D Ratio is : " + localDecimalFormat.format(d3); 

      if(!invalid){ 
      new AlertDialog.Builder(this) 
      .setTitle("K/D Ratio") 
      .setMessage(str3) 
      .setNeutralButton("Ok", null) 
      .show(); 
      } 
      invalid = false; 
     } 
} 

,並確保您輸入的計算器有這樣的2.0或2.2或類似的東西(雙格式)不要使用逗號...