Google地图 - 位置时出错>仅限设备

时间:2014-09-07 08:47:32

标签: android api gps maps

我有一个活动,上面有一个简单的地图。一切正常,直到我发现如果用户(激活GPS时)选择“仅设备 - 使用GPS确定您的位置”而不是“高精度 - 使用GPS,Wi-Fi和移动网络来确定位置”,它返回一个NullPointerException。

这是日志猫:

09-07 10:34:04.672: E/AndroidRuntime(24336): FATAL EXCEPTION: main
09-07 10:34:04.672: E/AndroidRuntime(24336): Process: com.lifeapps.speedcalc, PID: 24336
09-07 10:34:04.672: E/AndroidRuntime(24336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lifeapps.speedcalc/com.lifeapps.speedcalc.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
09-07 10:34:04.672: E/AndroidRuntime(24336):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
09-07 10:34:04.672: E/AndroidRuntime(24336):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
09-07 10:34:04.672: E/AndroidRuntime(24336):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-07 10:34:04.672: E/AndroidRuntime(24336):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-07 10:34:04.672: E/AndroidRuntime(24336):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-07 10:34:04.672: E/AndroidRuntime(24336):    at android.os.Looper.loop(Looper.java:136)
09-07 10:34:04.672: E/AndroidRuntime(24336):    at android.app.ActivityThread.main(ActivityThread.java:5001)
09-07 10:34:04.672: E/AndroidRuntime(24336):    at java.lang.reflect.Method.invoke(Native Method)
09-07 10:34:04.672: E/AndroidRuntime(24336):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-07 10:34:04.672: E/AndroidRuntime(24336):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-07 10:34:04.672: E/AndroidRuntime(24336): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
09-07 10:34:04.672: E/AndroidRuntime(24336):    at com.lifeapps.speedcalc.MainActivity.setUpMap(MainActivity.java:155)
09-07 10:34:04.672: E/AndroidRuntime(24336):    at com.lifeapps.speedcalc.MainActivity.setUpMapIfNeeded(MainActivity.java:116)
09-07 10:34:04.672: E/AndroidRuntime(24336):    at com.lifeapps.speedcalc.MainActivity.onCreate(MainActivity.java:46)
09-07 10:34:04.672: E/AndroidRuntime(24336):    at android.app.Activity.performCreate(Activity.java:5231)
09-07 10:34:04.672: E/AndroidRuntime(24336):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-07 10:34:04.672: E/AndroidRuntime(24336):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
09-07 10:34:04.672: E/AndroidRuntime(24336):    ... 9 more

所以,这个:

double latitude = lastKnownLocation.getLatitude();
double longitude = lastKnownLocation.getLongitude();

即使用户没有互联网连接,该应用仍可正常运行,但如果选择此选项,则无法加载。

如何避免此错误?我怎样才能使用try / catch来确保它不再发生?

1 个答案:

答案 0 :(得分:0)

我解决了我的问题,只需添加"如果lastknownlocation == null,则显示一个警告对话框,要求用户在位置模式中选择高精度"。

谢谢你,Kidd Bubu。