所以我有我的谷歌地图,我做的是从我的服务器上获取经纬度作为一个字符串,然后将它们转换成一个latlng位置。我遇到的问题是,如果手机没有找到GPS位置,它会在我的数据库中存储一个空字符串,然后当谷歌地图试图显示它时,它会让所有试图访问地图的用户崩溃我的应用程序。我收集的是空字符串,因为如果我从数据库中删除这个字符串,一切都会恢复正常。我需要知道的是从空字符串或空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 - Google Map崩溃null LatLng,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21397371/

10-12 02:42