Java 文本更改上的EditeText正在崩溃

Java 文本更改上的EditeText正在崩溃,java,android,android-edittext,Java,Android,Android Edittext,我必须将一些数字文本转换为双精度文本,并在文本更改后执行简单的数学操作。除了文本更改外,一切正常:无论我做什么,它都会崩溃(也可以用“hello world”更改文本视图) 这是我的密码: hEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int

我必须将一些数字文本转换为双精度文本,并在文本更改后执行简单的数学操作。除了文本更改外,一切正常:无论我做什么,它都会崩溃(也可以用“hello world”更改文本视图)

这是我的密码:

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.internal.os.ZygoteInit.main(ZygoteInit.java:832)

尝试将代码移动到:

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

正在尝试更改以下行:
Double hDouble=Double.parseDouble(hEditText.toString())
Double hDouble=Double.parseDouble(hEditText.getText().trim())和其他行

每当解析器不生成双精度代码时,您的应用程序就会崩溃。在try/catch中包含从String到Double的每个转换。如果用户键入除数字或以外的任何内容,或清除编辑文本
然后应用程序将崩溃

方法
后文本更改
无法崩溃,因为它是空的。另一方面,方法
onTextChanged
,它广泛使用了
Double.parseDouble(String)
,因为如果您给它一些不能转换为Double的东西,这将抛出一个
NumberFormatException
。看

为了避免这种情况,一种可能是创建一个helper方法,这样您就不会到处放置
try-catch
块,而是决定捕获异常并返回默认值(例如
0D
),或者让它传播

例如:

private double toDouble(String raw) {
    double val;
    try {
        val = Double.parseDouble(raw);
    } catch (NumberFormatException e) {
        val = 0D;
    }
    return val;
}
然后
onTextChanged
中的代码变为

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

添加崩溃日志?请在这里发布您的logcat堆栈跟踪。我想您的意思是说onTextChanged()不断崩溃,对吗?因为您的PostTextChanged()是空的,所以我在文本更改上的意思是,我添加了日志!文本字段是唯一的数字,在解析更改
val=Double.parseDouble(val)之前,请在
editText.getText().toString().isEmpty()时进行检查
val=Double.parseDouble(原始)@Arfmann我已经更新了我的答案。由于mTak指出的打字错误,它无法工作。
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) {

        }
    });