gpt4 book ai didi

java - NullPointerException - Location & LocationManager - 双

转载 作者:行者123 更新时间:2023-11-29 07:57:52 25 4
gpt4 key购买 nike

我从以下代码行中得到一个空指针异常:

    double longitude = location.getLongitude();

我无法弄清楚问题出在哪里。

    LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, waypointActivity);

//LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String locLat = String.valueOf(latitude)+","+String.valueOf(longitude);

这是 logcat 输出:

04-30 10:20:54.988: E/AndroidRuntime(1827): FATAL EXCEPTION: main
04-30 10:20:54.988: E/AndroidRuntime(1827): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.LoadedApk.makeApplication(LoadedApk.java:504)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4364)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.ActivityThread.access$1300(ActivityThread.java:141)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.os.Looper.loop(Looper.java:137)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-30 10:20:54.988: E/AndroidRuntime(1827): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 10:20:54.988: E/AndroidRuntime(1827): at java.lang.reflect.Method.invoke(Method.java:511)
04-30 10:20:54.988: E/AndroidRuntime(1827): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-30 10:20:54.988: E/AndroidRuntime(1827): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-30 10:20:54.988: E/AndroidRuntime(1827): at dalvik.system.NativeStart.main(Native Method)
04-30 10:20:54.988: E/AndroidRuntime(1827): Caused by: java.lang.NullPointerException
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:379)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.LoadedApk.getClassLoader(LoadedApk.java:322)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
04-30 10:20:54.988: E/AndroidRuntime(1827): ... 11 more
04-30 10:21:04.048: E/Trace(1863): error opening trace file: No such file or directory (2)

我不明白为什么它认为我的位置变量为空。如有任何建议,我们将不胜感激。

最佳答案

documentation状态:

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.

由于您的 GPS 位置为空,您可能没有打开 GPS 或者您的手机没有 GPS。

将您的代码更改为

Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null){
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String locLat = String.valueOf(latitude)+","+String.valueOf(longitude);
}

关于java - NullPointerException - Location & LocationManager - 双,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16303576/

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