我正在嘗試使用兩個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);
}
}
});
您可以顯示日誌錯誤? –
有沒有錯誤,它只是停止工作 –
必須有一個logcat消息... –