Android - App crashing because of TextWatcher

4.8k views Asked by At

When I write a number in the editText it works fine but when I delete the number the app crashes.

This is the code:

  final EditText et2 = (EditText) findViewById(R.id.editText2);
  final EditText et3 = (EditText) findViewById(R.id.editText3);

 et2.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            DecimalFormat df = new DecimalFormat("#.00");

            String et2string = et2.getText().toString();
            int et2int = Integer.parseInt(et2string);


            Double finpriceAdulto = (priceAdulto * et2int);

            TextView tvPriceAdulto = (TextView) findViewById(R.id.textView7);
            tvPriceAdulto.setText("" + df.format(finpriceAdulto));

        }
    });

et3.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            DecimalFormat df = new DecimalFormat("#.00");

            String et3string = et3.getText().toString();
            int et3int = Integer.parseInt(et3string);


            Double finpriceNino = (priceNino * et3int);

            TextView tvPriceNino = (TextView) findViewById(R.id.textView8);
            tvPriceNino.setText("" + df.format(finpriceNino));

        }
    });

And this is the logcat error:

06-26 13:32:07.835    6321-6321/com.app.boyd44.myfirstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.app.boyd44.myfirstapp, PID: 6321
java.lang.NumberFormatException: Invalid int: ""
        at java.lang.Integer.invalidInt(Integer.java:137)
        at java.lang.Integer.parseInt(Integer.java:358)
        at java.lang.Integer.parseInt(Integer.java:331)
        at com.app.boyd44.myfirstapp.MyActivity$1.afterTextChanged(MyActivity.java:68)
        at android.widget.TextView.sendAfterTextChanged(TextView.java:7445)
        at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9232)
        at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
        at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:212)
        at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:30)
        at android.text.method.BaseKeyListener.backspaceOrForwardDelete(BaseKeyListener.java:94)
        at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:49)
        at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:155)
        at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:138)
        at android.widget.TextView.doKeyDown(TextView.java:5553)
        at android.widget.TextView.onKeyDown(TextView.java:5360)
        at android.view.KeyEvent.dispatch(KeyEvent.java:2663)
        at android.view.View.dispatchKeyEvent(View.java:7796)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2039)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1509)
        at android.app.Activity.dispatchKeyEvent(Activity.java:2421)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1966)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3875)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3849)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3416)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3466)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3435)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3542)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3443)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3599)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3416)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3466)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3435)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3443)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3416)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5565)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5545)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5516)
        at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3187)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5212)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
        at dalvik.system.NativeStart.main(Native Method)

The crash has clearly something to do with TextWatcher because it crashes when deleting something in the editText.

I don't know how to fix this so please help.

6

There are 6 answers

0
55597 On BEST ANSWER

The textchanged gets called even when you delete the content. So when your content is empty. Check for the condition and return.

DecimalFormat df = new DecimalFormat("#.00");
if(s.toString().trim().length()==0){return}
            String et2string = et2.getText().toString();
            int et2int = Integer.parseInt(et2string);


            Double finpriceAdulto = (priceAdulto * et2int);

            TextView tvPriceAdulto = (TextView) findViewById(R.id.textView7);
            tvPriceAdulto.setText("" + df.format(finpriceAdulto));
0
Nagaraju V On

In afterTextChanged(-) just check length of the string as follows

@Override
    public void afterTextChanged(Editable s) {
       if(s.toString().trim().length()>0){
         //do your stuff here
      }
}

Hope this will helps you.

0
Pedro Valério On
int et3int = Integer.parseInt(et3string);

After you delete the number, afterTextChanged is invoked and in that method, this line attempts to convert an empty string to a number, thus generating a NumberFormatException.

0
Jambu On

The reason is you trying to convert string into int type.

  1. int et2int = Integer.parseInt("2"); //works
  2. int et2int = Integer.parseInt("sdf"); //will not work
0
Karan Chunara On

In afterTextChanged(-) just check length of the string Dynamic as follows

                @Override
            public void afterTextChanged(Editable s) {


                String c,d;
                c=textView1[flag2].getText().toString();
                d=Tltr.getText().toString();
                int a,b;
                if (c.toString().trim().length()>0)
                {
                    b=Integer.parseInt(c);
                    a=Integer.parseInt(d);
                    int total;
                    total=b*a;
                    ltrSpinner[flag3].setText(Integer.toString(total));
                }
                else {
                    ltrSpinner[flag3].setText("Ltr");
                }


            }
0
aangoshe On

I once have such kind of problem but i solve it after checking in my code:

I do

Public Static final string dbname="person.name";

Instead of

Public Static final string dbname="person_name";

Java supports underscore(_) in string but didn't support dots (.)