Android - 应用因 TextWatcher 而崩溃

Android - App crashing because of TextWatcher

当我在 editText 中写入一个数字时它工作正常但是当我删除数字时应用程序崩溃。

这是代码:

  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));

        }
    });

这是 logcat 错误:

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.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)

崩溃显然与 TextWatcher 有关,因为它在删除 editText 中的内容时崩溃。

我不知道如何解决这个问题,请帮忙。

afterTextChanged(-)中只需检查字符串的长度如下

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

希望对您有所帮助。

即使您删除了内容,textchanged 也会被调用。因此,当您的内容为空时。检查条件和 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));
int et3int = Integer.parseInt(et3string);

删除数字后,将调用 afterTextChanged,在该方法中,此行会尝试将空字符串转换为数字,从而生成 NumberFormatException。

原因是您试图将字符串转换为整型。

  1. int et2int = Integer.parseInt("2"); //有效
  2. int et2int = Integer.parseInt("sdf"); // 不会工作

在 afterTextChanged(-) 中,只需检查字符串 Dynamic 的长度,如下所示

                @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");
                }


            }

我曾经遇到过这样的问题,但我在查看我的代码后解决了它:

我愿意

Public 静态最终字符串 dbname="person.name";

而不是

Public 静态最终字符串 dbname="person_name";

Java 支持字符串中的下划线 (_) 但不支持点 (.)