NullPointerException - Location - getLatitude()方法

时间:2013-06-17 14:32:10

标签: java android android-location android-maps-v2

我在这一行收到空指针异常:

double latitude = location.getLatitude();

我有什么需要做的来初始化我的Location变量吗?我究竟做错了什么?

这是我的源代码:

    Location location;

    if (isGpsEnabled) { 
        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    }
    else { 
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }

    Log.i(TAG, "Provider is: "+provider);
    Log.i(TAG, "Location is: "+location);


    //Zooms into the current location when the activity is started
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();

这是我收到的logcat输出:

06-17 09:22:40.797: E/AndroidRuntime(12436): FATAL EXCEPTION: main
06-17 09:22:40.797: E/AndroidRuntime(12436): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.gpstracker/com.polaris.epicriders.Rides.RideTracking}: java.lang.NullPointerException
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.os.Looper.loop(Looper.java:137)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.ActivityThread.main(ActivityThread.java:5041)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at java.lang.reflect.Method.invokeNative(Native Method)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at java.lang.reflect.Method.invoke(Method.java:511)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at dalvik.system.NativeStart.main(Native Method)
06-17 09:22:40.797: E/AndroidRuntime(12436): Caused by: java.lang.NullPointerException
06-17 09:22:40.797: E/AndroidRuntime(12436):    at com.polaris.epicriders.Rides.RideTracking.onCreate(RideTracking.java:273)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.Activity.performCreate(Activity.java:5104)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-17 09:22:40.797: E/AndroidRuntime(12436):    ... 11 more

2 个答案:

答案 0 :(得分:2)

如果已获取任何位置,

getLastKnownLocation将返回null

来自doc

  

返回指示上一个已知位置的数据的位置   从给定的提供者处获得的修复。

     

这可以在不启动提供程序的情况下完成。请注意这一点   位置可能已过时,例如,如果设备已转动   关闭并搬到另一个地方。

     

如果当前禁用了提供程序,则返回null。

答案 1 :(得分:0)

好的,做这个有趣的测试。启动谷歌地图并检查。如果您的应用程序突然运行,谷歌地图会做一些事情来触发手机上运行的Android操作系统内的lastKnownLocation缓存的更新。

我做了以下操作,它与谷歌地图所做的kickstart结果相同。尝试一下。

    HomeScreen.getLocationManager().requestLocationUpdates(
    LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
        @Override
        public void onProviderEnabled(String provider) {
        }
        @Override
        public void onProviderDisabled(String provider) {
        }
        @Override
        public void onLocationChanged(final Location location) {
        }
    });

注意,我在调用中没有做任何事情而是监听器。你只需要在这个kickstart之后调用你的getLastKnownLocation。

OR

尝试使用LocationClient api,它们是新的。