java - NullPointerException - 位置 - getLatitude() 方法

标签 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

最佳答案

getLastKnownLocation 如果已获取任何位置,则返回 null

来自 doc

Returns a Location indicating the data from the last known location fix obtained from the given provider.

This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.

If the provider is currently disabled, null is returned.

关于java - NullPointerException - 位置 - getLatitude() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17150066/

相关文章:

c# - C#中的布局管理器

java - 调用另一个数组内的数组

android - 从 cordova 插件访问自定义布局资源

android 是否可以写保护我使用 StorageAccessFramework 创建的纯文本文件?

android - Android 地理围栏可以在没有网络连接且仅打开 GPS 的情况下工作吗?

java - 使用Univocity CSV解析器解析具有相同定义但列数不同的两个不同文件

java - DynamoDB自动附加到多个列表

android - 如何将 Realm 与 RxJava2 一起使用

Android 谷歌地图当前位置方位

android - shouldShowRequestPermissionRationale 和 requestPermissions 之间有什么区别?