我正在制作一款旨在转换不同单位长度的应用。假设您要将2米转换为厘米。您必须在EditText中输入值,其中inputType为numberDecimal,然后在微调器中选择meter,然后选择将其转换为另一个微调器中的厘米。我想让应用程序完全自动化,并决定添加一个TextWatcher。问题是当你删除EditText中的所有内容时,它显然不能乘以或除以任何东西(不是0,没有)并执行方法,因此它崩溃了应用程序。这个问题有什么解决方案吗?我试过这个,但它仍然崩溃了:
final EditText lengthEntry = (EditText) findViewById(R.id.enterLength);
lengthEntry.addTextChangedListener(new TextWatcher() {
final EditText lengthEntry = (EditText) findViewById(R.id.enterLength);
lengthEntry.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void afterTextChanged(Editable s) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (Double.valueOf(lengthEntry.getText().toString()) == ""){} else {
lengthConverter();
}
}
});
这是logcat:
FATAL EXCEPTION: main
Process: n1.theconverter, PID: 5585
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at java.lang.Double.valueOf(Double.java:338)
at n1.theconverter.MainActivity$3.afterTextChanged(MainActivity.java:87)
at android.widget.TextView.sendAfterTextChanged(TextView.java:8007)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:10168)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1043)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:560)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:225)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:224)
at android.view.inputmethod.BaseInputConnection.deleteSurroundingText(BaseInputConnection.java:244)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:389)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
提前谢谢你:)
编辑:新logcat:
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at java.lang.Double.valueOf(Double.java:338)
at n1.theconverter.MainActivity.lengthConverter(MainActivity.java:176)
at n1.theconverter.MainActivity$3.onTextChanged(MainActivity.java:91)
at android.widget.TextView.sendOnTextChanged(TextView.java:7991)
at android.widget.TextView.handleTextChanged(TextView.java:8053)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10157)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1033)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:559)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:225)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:224)
at android.text.method.BaseKeyListener.backspaceOrForwardDelete(BaseKeyListener.java:106)
at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:51)
at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:215)
at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:138)
at android.widget.TextView.doKeyDown(TextView.java:6098)
at android.widget.TextView.onKeyDown(TextView.java:5911)
at android.view.KeyEvent.dispatch(KeyEvent.java:2640)
at android.view.View.dispatchKeyEvent(View.java:9234)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2395)
at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1727)
at android.app.Activity.dispatchKeyEvent(Activity.java:2725)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:541)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:319)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2310)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4127)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4089)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forwa
04-19 20:38:03.451 6053-6053/n1.theconverter D/AndroidRuntime: Shutting down VM
---------
beginning of crash
04-19 20:38:03.451 6053-6053/n1.theconverter E/AndroidRuntime: FATAL EXCEPTION: main
Process: n1.theconverter, PID: 6053
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at java.lang.Double.valueOf(Double.java:338)
at n1.theconverter.MainActivity.lengthConverter(MainActivity.java:176)
at n1.theconverter.MainActivity$3.onTextChanged(MainActivity.java:91)
at android.widget.TextView.sendOnTextChanged(TextView.java:7991)
at android.widget.TextView.handleTextChanged(TextView.java:8053)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10157)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1033)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:559)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:225)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:224)
at android.text.method.BaseKeyListener.backspaceOrForwardDelete(BaseKeyListener.java:106)
at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:51)
at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:215)
at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:138)
at android.widget.TextView.doKeyDown(TextView.java:6098)
at android.widget.TextView.onKeyDown(TextView.java:5911)
at android.view.KeyEvent.dispatch(KeyEvent.java:2640)
at android.view.View.dispatchKeyEvent(View.java:9234)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2395)
at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1727)
at android.app.Activity.dispatchKeyEvent(Activity.java:2725)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:541)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:319)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2310)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4127)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4089)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3
答案 0 :(得分:2)
java.lang.NumberFormatException:无效的double:“”
您无法将空字符串与Double
值进行比较。您仍然可以检查editText
是否有任何值:如果是,则调用您的计算方法。试试这个:
private EditText lengthEntry; //Declare globally
lengthEntry = (EditText) findViewById(R.id.enterLength);
lengthEntry.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!lengthEntry.getText().toString().equals("")){
lengthConverter();
}
}
希望这有帮助。
答案 1 :(得分:0)
我会在这做两件事: 首先确保THE LENGTH>第二个是检查以确保它确实是可变的。例如不只是' - '或'。'。但是类似的事情 - #。#
答案 2 :(得分:0)
确保lengthEntry
具有输入字段的正确ID。你可以尝试System.out.println();
来查看长度和输入。
尝试按Doube.parseDouble(lenghtEntry.getText().toString());
解析值。
如果同样的问题和ID不正确。
答案 3 :(得分:0)
更改您的条件,您的onTextChange
应如下:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.equals(""){
lengthEntry.setText("0");
} else {
lengthConverter();
}
}
答案 4 :(得分:0)
我找到了解决问题的方法。 if
方法中的布尔值不正确。以下是使用isEmpty()
方法执行此操作的正确方法:
if (lengthEntry.getText().toString().isEmpty()){
tvAnswer.setText("0.0");
} else {
lengthConverter();
}
非常感谢!