gpt4 book ai didi

java - EditText 文本更改崩溃

转载 作者:行者123 更新时间:2023-12-02 11:03:26 25 4
gpt4 key购买 nike

我必须将一些数字 editText 转换为 double ,并在文本更改后执行简单的数学运算。除了文本更改之外,一切正常:无论我做什么,它都会崩溃(也可以用“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)

最佳答案

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

}
});

关于java - EditText 文本更改崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51160762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com