gpt4 book ai didi

android - 谷歌地图崩溃 null LatLng

转载 作者:太空狗 更新时间:2023-10-29 14:16:34 25 4
gpt4 key购买 nike

所以我有我的谷歌地图,我所做的是从我的服务器获取纬度和经度作为字符串,然后我将它们转换为 LatLng 位置。我遇到的问题是,如果手机没有获取 gps 位置,它会在我的数据库中存储一个空字符串,然后当谷歌地图试图显示它时,它会导致所有试图访问 map 的用户的应用程序崩溃。我收集的是空字符串,因为如果我从数据库中删除那个字符串,一切都会再次正常工作。我需要知道的是检查空字符串或 null LatLng 并跳过它以免崩溃的最佳方法。

已更新编辑代码

for (int i = 0; i < marker_list.size(); i+=2)
{
String lat = marker_list.get(i);
String lng = marker_list.get(i+1);

// NEED TO MAKE SURE CHECK BEFORE HERE AND IF LOCATION DOESN'T EXIST SKIP TO NEXT.
if (lat == "" || lng == "")
{
continue;
}
else
{
double lat_ = Double.valueOf(lat);
double lng_ = Double.valueOf(lng);

LatLng location = new LatLng(lat_, lng_);

map.addMarker(new MarkerOptions()
.position(location)
.title(title)
.snippet(index)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.event)));
}

}


01-28 00:33:58.060: E/AndroidRuntime(919): FATAL EXCEPTION: main
01-28 00:33:58.060: E/AndroidRuntime(919): Process: com.databasedemo, PID: 919
01-28 00:33:58.060: E/AndroidRuntime(919): java.lang.NumberFormatException: Invalid double: ""
01-28 00:33:58.060: E/AndroidRuntime(919): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
01-28 00:33:58.060: E/AndroidRuntime(919): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
01-28 00:33:58.060: E/AndroidRuntime(919): at java.lang.Double.parseDouble(Double.java:295)
01-28 00:33:58.060: E/AndroidRuntime(919): at java.lang.Double.valueOf(Double.java:332)
01-28 00:33:58.060: E/AndroidRuntime(919): at com.databasedemo.Map$PrefetchData.onPostExecute(Map.java:257)
01-28 00:33:58.060: E/AndroidRuntime(919): at com.databasedemo.Map$PrefetchData.onPostExecute(Map.java:1)
01-28 00:33:58.060: E/AndroidRuntime(919): at android.os.AsyncTask.finish(AsyncTask.java:632)
01-28 00:33:58.060: E/AndroidRuntime(919): at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-28 00:33:58.060: E/AndroidRuntime(919): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
01-28 00:33:58.060: E/AndroidRuntime(919): at android.os.Handler.dispatchMessage(Handler.java:102)
01-28 00:33:58.060: E/AndroidRuntime(919): at android.os.Looper.loop(Looper.java:136)
01-28 00:33:58.060: E/AndroidRuntime(919): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-28 00:33:58.060: E/AndroidRuntime(919): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 00:33:58.060: E/AndroidRuntime(919): at java.lang.reflect.Method.invoke(Method.java:515)
01-28 00:33:58.060: E/AndroidRuntime(919): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-28 00:33:58.060: E/AndroidRuntime(919): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-28 00:33:58.060: E/AndroidRuntime(919): at dalvik.system.NativeStart.main(Native Method)

最佳答案

尝试如下:

     String lat = marker_list.get(i);
String lng = marker_list.get(i+1);
if(!lat.isEmpty() && !lng.isEmpty())
{
try{
double lat_ = Double.parseDouble(lat);
double lng_ = Double.parseDouble(lng);
}catch(NumberFormatException e){}
}

关于android - 谷歌地图崩溃 null LatLng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21397371/

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