Android Studio 3.6,java 1.8。
在我拥有的android项目中:
double newAmountIntValueAsDouble = Double.parseDouble(newAmountValueAsString);
但我收到错误消息:
12-30 09:28:50.412 E/AndroidRuntime(21915): java.lang.IllegalStateException: Could not execute method for android:onClick
12-30 09:28:50.412 E/AndroidRuntime(21915): at android.view.View$DeclaredOnClickListener.onClick(View.java:4458)
12-30 09:28:50.412 E/AndroidRuntime(21915): at android.view.View.performClick(View.java:5204)
12-30 09:28:50.412 E/AndroidRuntime(21915): at android.view.View$PerformClick.run(View.java:21153)
12-30 09:28:50.412 E/AndroidRuntime(21915): at android.os.Handler.handleCallback(Handler.java:739)
12-30 09:28:50.412 E/AndroidRuntime(21915): at android.os.Handler.dispatchMessage(Handler.java:95)
12-30 09:28:50.412 E/AndroidRuntime(21915): at android.os.Looper.loop(Looper.java:148)
12-30 09:28:50.412 E/AndroidRuntime(21915): at android.app.ActivityThread.main(ActivityThread.java:5417)
12-30 09:28:50.412 E/AndroidRuntime(21915): at java.lang.reflect.Method.invoke(Native Method)
12-30 09:28:50.412 E/AndroidRuntime(21915): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-30 09:28:50.412 E/AndroidRuntime(21915): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-30 09:28:50.412 E/AndroidRuntime(21915): Caused by: java.lang.reflect.InvocationTargetException
12-30 09:28:50.412 E/AndroidRuntime(21915): at java.lang.reflect.Method.invoke(Native Method)
12-30 09:28:50.412 E/AndroidRuntime(21915): at android.view.View$DeclaredOnClickListener.onClick(View.java:4453)
12-30 09:28:50.412 E/AndroidRuntime(21915): ... 9 more
12-30 09:28:50.412 E/AndroidRuntime(21915): Caused by: java.lang.NumberFormatException: Invalid double: "2.188558E"
12-30 09:28:50.412 E/AndroidRuntime(21915): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
12-30 09:28:50.412 E/AndroidRuntime(21915): at java.lang.StringToReal.initialParse(StringToReal.java:94)
12-30 09:28:50.412 E/AndroidRuntime(21915): at java.lang.StringToReal.parseDouble(StringToReal.java:282)
12-30 09:28:50.412 E/AndroidRuntime(21915): at java.lang.Double.parseDouble(Double.java:301)
12-30 09:28:50.412 E/AndroidRuntime(21915): at com.myproject.mvp.presenter.CalcPaymentPresenter.doClickSpecButton(CalcPaymentPresenter.java:144)
12-30 09:28:50.412 E/AndroidRuntime(21915): at com.myproject.ui.CalcPaymentActivity.onClickSpecButton(CalcPaymentActivity.java:106)
12-30 09:28:50.412 E/AndroidRuntime(21915): ... 11 more
答案 0 :(得分:0)
Caused by: java.lang.NumberFormatException: Invalid double: "2.188558E"
您的实际数字看起来像是在末尾被切开,在E后面应该有一个数字,因为这是科学计数法。
答案 1 :(得分:0)
原因是“ 2.188558E”不是有效的双精度数。您需要先测试String或捕获此类异常
double w;
try {
w = new Double(input3.getText().toString());
} catch (NumberFormatException e) {
w = 0; // your default value
}
答案 2 :(得分:0)
最后一位(或两位)似乎被截断了。 “ 2.188558E”应该是在“ 2.188558E-2”上的某行或类似内容。检查字符串的来源以查找问题。
有关双重表示法,请参见this answer for more info
答案 3 :(得分:0)
由于您正在使用科学计数法分析字符串,因此必须执行以下操作:
Double.parseDouble("2.188558E+1");