Android Studio-更改文字后会崩溃

时间:2018-07-03 18:16:54

标签: java android android-studio android-edittext

我必须将一些数字editText转换为双精度,并且在更改文本后执行简单的数学运算。一切正常,除了“更改文本”:我执行任何操作都会崩溃(也可以使用“ 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.internal.os.ZygoteInit.main(ZygoteInit.java:832)

5 个答案:

答案 0 :(得分:1)

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

        }
    });

答案 1 :(得分:0)

尝试将代码移至:

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

每次解析器不生成Double时,您的应用都会崩溃。在try / catch中将从String到Double的每个转换括起来。如果用户键入数字或。以外的任何其他字符,或清除EditText  然后该应用将崩溃

答案 4 :(得分:0)

方法afterTextChanged无法崩溃,因为它为空。另一方面,方法onTextChanged绝对可以广泛使用Double.parseDouble(String),因为如果您提供了不能转换为NumberFormatException的内容,则会抛出try-catch。双。参见documentation

要解决此问题,一种可能性是创建一个辅助方法,以使您不会在各处放置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))); } 中的代码变为

string query = 'select id from ClientData__c';
string Point05 = '%\\file-01\projects\Internal Audit\Internal Audit 
Team\FY18\SOX\Testing%';

query += ' WHERE Point05__c LIKE \'' + Point05 + '\'';

List<ClientData__c> clientData = database.query(query);