代码之家  ›  专栏  ›  技术社区  ›  Arfmann

android studio-on text changed正在崩溃

  •  0
  • Arfmann  · 技术社区  · 6 年前

    我必须将一些数字编辑文本转换为双精度文本,在文本更改后执行一个简单的数学运算。除了文本更改之外,一切都可以正常工作:它崩溃了我所做的一切(还可以用“hello world”更改textview)。

    这是我的代码:

    hEditText.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)
            {
                Double hDouble = Double.parseDouble(hEditText.toString());
                Double bDouble = Double.parseDouble(bEditText.toString());
                Double mDouble = Double.parseDouble(mEditText.getText().toString());
                Double miDouble = Double.parseDouble(miEditText.getText().toString());
    
                lResult.setText("" + Math.sqrt((hDouble * hDouble) + (bDouble * bDouble)));
            }
    
            @Override
            public void afterTextChanged(Editable editable) {
    
            }
        });
    

    原木

    E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.cosmo.fisicapp, PID: 21198
                  java.lang.NumberFormatException: empty String
                      at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
                      at java.lang.Double.parseDouble(Double.java:547)
                      at com.example.cosmo.fisicapp.Equilibrio$1.afterTextChanged(Equilibrio.java:47)
                      at android.widget.TextView.sendAfterTextChanged(TextView.java:8525)
                      at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:10788)
                      at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1222)
                      at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:583)
                      at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:509)
                      at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:508)
                      at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
                      at android.widget.TextView.doKeyDown(TextView.java:6533)
                      at android.widget.TextView.onKeyDown(TextView.java:6323)
                      at android.view.KeyEvent.dispatch(KeyEvent.java:2742)
                      at android.view.View.dispatchKeyEvent(View.java:9949)
                      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1667)
                      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1667)
                      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1667)
                      at android.widget.ScrollView.dispatchKeyEvent(ScrollView.java:391)
                      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1667)
                      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1667)
                      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1667)
                      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1667)
                      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1667)
                      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1667)
                      at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:439)
                      at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1861)
                      at android.app.Activity.dispatchKeyEvent(Activity.java:3141)
                      at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:535)
                      at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
                      at android.support.v7.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:2530)
                      at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:353)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4742)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4713)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4249)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4302)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4268)
                      at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4395)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4276)
                      at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4452)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4249)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4302)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4268)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4276)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4249)
                      at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6676)
                      at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6650)
                      at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6611)
                      at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3917)
                      at android.os.Handler.dispatchMessage(Handler.java:105)
                      at android.os.Looper.loop(Looper.java:156)
                      at android.app.ActivityThread.main(ActivityThread.java:6523)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
    

    E/ANDROIDRunTime:在COM. Android .No.OS.ZyGoTeInIt . My.(ZyGoTynIt.java:832)

    5 回复  |  直到 6 年前
        1
  •  1
  •   Dhara Jani    6 年前
    hEditText.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)
            {
              if(!charSequence.equals("")){
                Double hDouble = Double.parseDouble(charSequence.toString());
                Double bDouble = Double.parseDouble(charSequence.toString());
                Double mDouble = Double.parseDouble(charSequence.getText().toString());
                Double miDouble = Double.parseDouble(charSequence.getText().toString());
    
                lResult.setText("" + Math.sqrt((hDouble * hDouble) + (bDouble * bDouble)));
               }
            }
    
            @Override
            public void afterTextChanged(Editable editable) {
    
            }
        });
    
        2
  •  0
  •   Sahil    6 年前

        @Override
        public void afterTextChanged(Editable editable) {
            Double hDouble = Double.parseDouble(hEditText.toString());
            Double bDouble = Double.parseDouble(bEditText.toString());
            Double mDouble = Double.parseDouble(mEditText.getText().toString());
            Double miDouble = Double.parseDouble(miEditText.getText().toString());
    
            lResult.setText("" + Math.sqrt((hDouble * hDouble) + (bDouble * bDouble)));
        }
    
        3
  •  0
  •   Ezekiel Sebastine    6 年前

    Double hDouble = Double.parseDouble(hEditText.toString()); Double hDouble = Double.parseDouble(hEditText.getText().trim());

        4
  •  0
  •   user8959091    6 年前

        5
  •  0
  •   geco17    6 年前

    afterTextChanged onTextChanged Double.parseDouble(String) NumberFormatException documentation

    try-catch 0D

    private double toDouble(String raw) {
        double val;
        try {
            val = Double.parseDouble(raw);
        } catch (NumberFormatException e) {
            val = 0D;
        }
        return val;
    }
    

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
        {
            Double hDouble = toDouble(hEditText.toString());
            Double bDouble = toDouble(bEditText.toString());
            Double mDouble = toDouble(mEditText.getText().toString());
            Double miDouble = toDouble(miEditText.getText().toString());
    
            lResult.setText("" + Math.sqrt((hDouble * hDouble) + (bDouble * bDouble)));
        }