2017-07-03 61 views
-4

我正在嘗試使用兩個EditText字段創建一個簡單的時間計算器應用程序。下面的代碼只是一個普通的計算器,它加起來兩個數字,我鞭打測試文本更改監聽器。出於某種原因,每次運行此應用程序並在EditText字段中輸入數字時,它都會崩潰並詢問我是否要重新啓動應用程序。每次用戶輸入一個數字時,我只想讓它更新總數,而不必按下按鈕。有人可以幫我找到錯誤嗎?我已經初始化了onCreate方法之外的前幾個變量。EditText AfterTextChanged問題

日誌錯誤:

07-03 21:06:45.499 2397-2397/com.example.thesoulreaper.swimmingcalculator E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.example.thesoulreaper.swimmingcalculator, PID: 2397 
    java.lang.NumberFormatException: empty String 
     at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071) 
     at java.lang.Float.parseFloat(Float.java:459) 
     at com.example.thesoulreaper.swimmingcalculator.OneHundredCalculatorActivity$3.afterTextChanged(OneHundredCalculatorActivity.java:103) 
     at android.widget.TextView.sendAfterTextChanged(TextView.java:8202) 
     at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:10381) 
     at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1218) 
     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:579) 
     at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:230) 
     at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:229) 
     at android.view.inputmethod.BaseInputConnection.deleteSurroundingText(BaseInputConnection.java:251) 
     at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:451) 
     at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:91) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:154) 
     at android.app.ActivityThread.main(ActivityThread.java:6077) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

我的代碼:

fiftyEditText = (EditText) findViewById(R.id.FiftyEditText); 
    hundredEditText = (EditText) findViewById(R.id.HundredEditText); 
    totalTextView = (TextView) findViewById(R.id.totalTextView); 

    fiftyEditText.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

     } 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

     } 

     @Override 
     public void afterTextChanged(Editable editable) { 
      if(Integer.parseInt(hundredEditText.getText().toString()) > 0 && Integer.parseInt(fiftyEditText.getText().toString()) > 0) { 
       int total = Integer.parseInt(hundredEditText.getText().toString()) + Integer.parseInt(fiftyEditText.getText().toString()); 
       totalTextView.setText(total); 
      } else if(Integer.parseInt(hundredEditText.getText().toString()) <= 0 && Integer.parseInt(fiftyEditText.getText().toString()) > 0) { 
       totalTextView.setText(Integer.parseInt(fiftyEditText.getText().toString())); 
      } else if(Integer.parseInt(hundredEditText.getText().toString()) > 0 && Integer.parseInt(fiftyEditText.getText().toString()) <= 0) { 
       totalTextView.setText(Integer.parseInt(hundredEditText.getText().toString())); 
      } else if (Integer.parseInt(hundredEditText.getText().toString()) <= 0 && Integer.parseInt(fiftyEditText.getText().toString()) <= 0) { 
       totalTextView.setText(0); 
      } 
     } 
    }); 

    hundredEditText.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

     } 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

     } 

     @Override 
     public void afterTextChanged(Editable editable) { 
      if(Integer.parseInt(hundredEditText.getText().toString()) > 0 && Integer.parseInt(fiftyEditText.getText().toString()) > 0) { 
       int total = Integer.parseInt(hundredEditText.getText().toString()) + Integer.parseInt(fiftyEditText.getText().toString()); 
       totalTextView.setText(total); 
      } else if(Integer.parseInt(hundredEditText.getText().toString()) <= 0 && Integer.parseInt(fiftyEditText.getText().toString()) > 0) { 
       totalTextView.setText(Integer.parseInt(fiftyEditText.getText().toString())); 
      } else if(Integer.parseInt(hundredEditText.getText().toString()) > 0 && Integer.parseInt(fiftyEditText.getText().toString()) <= 0) { 
       totalTextView.setText(Integer.parseInt(hundredEditText.getText().toString())); 
      } else if (Integer.parseInt(hundredEditText.getText().toString()) <= 0 && Integer.parseInt(fiftyEditText.getText().toString()) <= 0) { 
       totalTextView.setText(0); 
      } 
     } 
    }); 
+0

您可以顯示日誌錯誤? –

+0

有沒有錯誤,它只是停止工作 –

+0

必須有一個logcat消息... –

回答

0

您的應用程序崩潰,因爲你正在嘗試空字符串到整數所以加一個檢查,如果該字符串不是空的轉換

@Override 
public void afterTextChanged(Editable editable) { 

    int hundredEditTextValue = hundredEditText.getText().toString().isEmpty() ? 0 : Integer.parseInt(hundredEditText.getText().toString()); 
    int fiftyEditTextValue = fiftyEditText.getText().toString().isEmpty() ? 0 : Integer.parseInt(fiftyEditText.getText().toString()); 

    if(hundredEditTextValue > 0 && fiftyEditTextValue > 0) { 
     int total = hundredEditTextValue + fiftyEditTextValue; 
     totalTextView.setText(String.valueOf(total)); 
    } else if(hundredEditTextValue <= 0 && fiftyEditTextValue > 0) { 
     totalTextView.setText(String.valueOf(fiftyEditTextValue)); 
    } else if(hundredEditTextValue > 0 && fiftyEditTextValue <= 0) { 
     totalTextView.setText(String.valueOf(hundredEditTextValue)); 
    } else if (hundredEditTextValue <= 0 && fiftyEditTextValue <= 0) { 
     totalTextView.setText(String.valueOf(0)); 
    } 
} 

只是爲了讓你的edittext只接受數字在你的xml中添加這個

安卓的inputType = 「號」

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    . 
    . 
    android:inputType="number" /> 
+0

謝謝,它很好 –

+0

請upvote如果它幫助你的答案。 –