错误:引起:java.lang.NumberFormatException:无效的double:“”

时间:2015-07-06 09:13:46

标签: android expandablelistview numberformatexception

如何将字符串转换为double?

MainActivity.java

Location savedLocation = new Location("databaseLocation");
                savedLocation.setLatitude(Double.parseDouble(latitude));

logcat的:

07-06 14:41:21.870: E/AndroidRuntime(24097): FATAL EXCEPTION: main
07-06 14:41:21.870: E/AndroidRuntime(24097): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.locademo/com.example.locademo.MainActivity}: java.lang.NumberFormatException: Invalid double: ""
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.ActivityThread.access$700(ActivityThread.java:140)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.os.Looper.loop(Looper.java:177)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.ActivityThread.main(ActivityThread.java:4947)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at java.lang.reflect.Method.invokeNative(Native Method)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at java.lang.reflect.Method.invoke(Method.java:511)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at dalvik.system.NativeStart.main(Native Method)
07-06 14:41:21.870: E/AndroidRuntime(24097): Caused by: java.lang.NumberFormatException: Invalid double: ""
07-06 14:41:21.870: E/AndroidRuntime(24097):    at java.lang.StringToReal.invalidReal(StringToReal.java:63)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at java.lang.StringToReal.parseDouble(StringToReal.java:248)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at java.lang.Double.parseDouble(Double.java:295)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at com.example.locademo.MainActivity.getExpandableListData(MainActivity.java:203)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at com.example.locademo.MainActivity.onCreate(MainActivity.java:107)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.Activity.performCreate(Activity.java:5207)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-06 14:41:21.870: E/AndroidRuntime(24097):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)
07-06 14:41:21.870: E/AndroidRuntime(24097):    ... 11 more

3 个答案:

答案 0 :(得分:3)

错误清楚地显示您的值为空

java.lang.NumberFormatException: Invalid double: ""

您需要添加一个这样的条件

if (!latitude.equals("")){
Location savedLocation = new Location("databaseLocation");
                savedLocation.setLatitude(Double.parseDouble(latitude));
}

注意: 每当您进行数字格式转换时,请确保您没有获得任何空值或空值

答案 1 :(得分:1)

Double.parseDouble(String)工作正常,但如果提供的值无法转换为NumberFormatException,则会抛出double。参考stacktrace的第二行:

  

java.lang.NumberFormatException:无效的double:""

表示在这种情况下latitude的值为""(空字符串),转换失败。您可以对空字符串(空字符串或 null )实施一些检查,在到达转换之前修复latitude的值,或者catch异常并处理它。

答案 2 :(得分:-1)

检查您的纬度值是否为空或空白。

if (!latitude.equals("")){
// apply your logic
}