java NullPointerException - Location & LocationManager - double

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16303576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:34:16  来源:igfitidea点击:

NullPointerException - Location & LocationManager - double

javaandroidnullpointerexception

提问by user268397

I'm getting a null pointer exception from the following line of code:

我从以下代码行收到空指针异常:

    double longitude = location.getLongitude();

I having an issue figuring out what the problem is.

我在弄清楚问题是什么时遇到了问题。

    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);

Here is the logcat output:

这是 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.access00(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)

I can't understand why it thinks my location variable is null. Any suggestions would be appreciated.

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

回答by DigCamara

The documentationstates:

文件规定:

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.

返回一个位置,指示从给定提供程序获得的最后一个已知位置修复的数据。

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

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

Since you're getting null for GPS location, you probably have not turned on your GPS or your phone has no GPS.

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

Change your code to

将您的代码更改为

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);
}

回答by bakriOnFire

The LocationManager.GPS_PROVIDER(accessing location using phone's GPS) takes some time to access location, due to which you are getting location as null from locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)

LocationManager.GPS_PROVIDER(利用手机的GPS定位存取)需要一些时间来访问的位置,因为你越来越位置为空从locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)

Make your location object global and in your waypointActivity initialize this object in the implemented onLocationChangedmethod of LocationListener

使您的位置对象成为全局对象,并在您的 waypointActivity 中在实现的onLocationChanged方法中初始化此对象LocationListener

Something like this:-

像这样的东西:-

@Override
public void onLocationChanged(Location location) {
    this.location = location;
}

then check if the location is not null before using it as:-

然后在使用它之前检查该位置是否不为空:-

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

I would recommend you use LocationManager.NETWORK_PROVIDERinsted, because it gives the result much faster than GPS_PROVIDER.

我建议您使用LocationManager.NETWORK_PROVIDERinsted,因为它比GPS_PROVIDER.

回答by SSQ

This is not a "fix" but more a "workaround":

这不是“修复”,而是“解决方法”:

I have the same issue with my GPS location, though mine seems to be 'the first location returned after reinstalling the app via adb' which makes sense that the provider had not initialised yet. (as reinstalling the app removes any previous known location)

我的 GPS 位置也有同样的问题,尽管我的位置似乎是“通过 adb 重新安装应用程序后返回的第一个位置”,这说明提供商尚未初始化。(因为重新安装应用程序会删除任何以前的已知位置)

This results in whatever GPS task I was trying to accomplish always failing and returning null the first time it was triggered (all providers), but working as expected when restarted.

这导致我试图完成的任何 GPS 任务总是失败并在第一次触发时返回 null(所有提供者),但在重新启动时按预期工作。

I was advised by my supervisor and college to simply workaround the issue by simply beginning gps updates and throwing away the first location, moving immediately onto the next and proceeding from there as normal (detecting and/or handling any null location returned from then onwards as unexpected behaviour)

我的主管和大学建议我简单地解决这个问题,只需开始 gps 更新并丢弃第一个位置,立即移动到下一个位置并照常从那里继续(检测和/或处理从那时起返回的任何空位置)意外行为)

While this serves the purpose for me, i'd still be interested in how this is actuallysupposed to be best-practice solved?

虽然这对我有用,但我仍然对这实际上应该如何解决最佳实践感兴趣?

Hope this helps someone continue working in the meantime.

希望这有助于有人在此期间继续工作。