1

我创建了一个与 Web 服务交互的 android 应用程序。

我收到以下错误:

01-29 10:50:09.879: E/AndroidRuntime(12860): java.lang.RuntimeException: Unable to start activity ComponentInfo{aid.assistant233/aid.assistant233.AidAssistant233Activity}: java.lang.NumberFormatException: 
01-29 10:50:09.879: E/AndroidRuntime(12860):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at android.os.Looper.loop(Looper.java:130)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at android.app.ActivityThread.main(ActivityThread.java:3691)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at java.lang.reflect.Method.invokeNative(Native Method)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at java.lang.reflect.Method.invoke(Method.java:507)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at dalvik.system.NativeStart.main(Native Method)
01-29 10:50:09.879: E/AndroidRuntime(12860): Caused by: java.lang.NumberFormatException: 
01-29 10:50:09.879: E/AndroidRuntime(12860):    at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at java.lang.Double.parseDouble(Double.java:318)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at aid.assistant233.AidAssistant233Activity.SaveMapsToLocalDB(AidAssistant233Activity.java:1149)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at aid.assistant233.AidAssistant233Activity.UpdateApplicationData(AidAssistant233Activity.java:1060)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at aid.assistant233.AidAssistant233Activity.UpdateFromServer(AidAssistant233Activity.java:480)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at aid.assistant233.AidAssistant233Activity.onCreate(AidAssistant233Activity.java:212)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
01-29 1

0:50:09.879: E/AndroidRuntime(12860): ... 11 更多

我认为主要问题与以下有关:

01-29 10:50:09.879: E/AndroidRuntime(12860): Caused by: java.lang.NumberFormatException: 
01-29 10:50:09.879: E/AndroidRuntime(12860):    at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
01-29 10:50:09.879: E/AndroidRuntime(12860):    at java.lang.Double.parseDouble(Double.java:318)

相关代码是这样的:

for (int i = 0; i < dati.getLength(); i++) {
                    Element element = (Element) dati.item(i);
                    localDB.insertMap(XmlElemValue(element, "Title"), Double.parseDouble(XmlElemValue(element, "Latitude")),  // line 1149
                            Double.parseDouble(XmlElemValue(element, "Longitude")), Integer.parseInt(XmlElemValue(element, "MetriDistanza")),
                            Integer.parseInt(XmlElemValue(element, "MinutiIntervalloControllo")));
                }

webservice返回的xml中的标签纬度为:

<Latitude>43.5729</Latitude>
4

3 回答 3

2

问题出在调用时的关联代码中

Double.parseDouble(XmlElemValue(element, "Latitude"))

A NumberFormatException如果字符串不包含可解析的双精度则抛出。

您必须先检查该值,然后再将其传递给parseDouble. 也许纬度值是以度分秒 (DMS) 格式提供的。

于 2013-01-29T10:00:18.890 回答
0

Line 1149你确定是一个(XmlElemValue(element, "Latitude")包含. integer/double问题是您解析的值不是整数/双精度值。这就是为什么你得到Number Format Exception. 通过打印检查该行。

于 2013-01-29T10:03:43.227 回答
0

您的语言环境可能已设置为 Java 需要小数点逗号而不是小数点。尝试这个:

NumberFormat nf = NumberFormat.getInstance(Locale.US);
Double doubleValue = nf.parse(XmlElemValue(element, "Latitude")).doubleValue();
于 2013-01-29T10:08:23.353 回答