gpt4 book ai didi

java - 在 Android 中解析 double 时出错

转载 作者:行者123 更新时间:2023-11-29 21:50:18 24 4
gpt4 key购买 nike

我创建了一个与网络服务交互的 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>

最佳答案

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

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

如果字符串不包含可解析的 double ,则抛出 NumberFormatException

您必须在将值传递给 parseDouble 之前检查该值。也许纬度值以度分秒 (DMS) 格式提供。

关于java - 在 Android 中解析 double 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14580115/

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