2017-07-03 4 views
-4

2つのEditTextフィールドを使用して簡単な電卓アプリを作成しようとしています。以下のコードは、テキストが変更されたリスナーをテストするために2つの数値を加算した通常の電卓です。何らかの理由で、このアプリケーションを実行していずれかのEditTextフィールドに数値を入力するたびに、クラッシュし、アプリケーションを再起動するかどうか尋ねられます。ユーザーがボタンを押す必要がなく、数字を入力するたびに合計を更新したいだけです。誰かがエラーを見つけるのを助けることができますか?私は最初のいくつかの変数をonCreateメソッドの外で初期化しました。EditText AfterTextChanged Issue

・ログ・エラー:

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

ログキャッチメッセージが必要です。 –

答えて

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

助けがあれば回答をアップしてください。 –

関連する問題