停止 LocationListener 服务时出现问题

发布于 2024-12-01 05:00:35 字数 2284 浏览 1 评论 0原文

我有一个实现 LocationListener 的 IntentService。它在我的应用程序运行时运行,并在我的应用程序关闭时被销毁。在其 onHandleIntent 方法中,我获取一个 LocationManager 并注册服务表单位置更新。在其 onDestroy 方法中,我调用:

locationManager.removeUpdates(this);

但是,这有时会导致 NullPointerException:

08-23 20:45:09.826: ERROR/AndroidRuntime(6541): FATAL EXCEPTION: main
08-23 20:45:09.826: ERROR/AndroidRuntime(6541): java.lang.RuntimeException: Unable to stop service uk.ac.ic.doc.vmw10.wherewolf.helpers.Locater@44f53fa8: java.lang.NullPointerException
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at android.app.ActivityThread.handleStopService(ActivityThread.java:3090)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at android.app.ActivityThread.access$3700(ActivityThread.java:125)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2099)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at android.os.Looper.loop(Looper.java:123)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at java.lang.reflect.Method.invokeNative(Native Method)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at java.lang.reflect.Method.invoke(Method.java:521)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at dalvik.system.NativeStart.main(Native Method)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541): Caused by: java.lang.NullPointerException
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at uk.ac.ic.doc.vmw10.wherewolf.helpers.Locater.onDestroy(Locater.java:137)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at android.app.ActivityThread.handleStopService(ActivityThread.java:3076)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     ... 10 more

如果删除该行,NullPointerException 就会消失,但当我关闭应用程序时,LocationListener 不会停止。我有什么想法可以解决这个问题吗?

I have an IntentService that implements LocationListener. It runs while my application is running, and is destroyed when my application is closed. In its onHandleIntent method I get a LocationManager and register the service form location updates. In its onDestroy method, I call:

locationManager.removeUpdates(this);

However, this sometimes causes a NullPointerException:

08-23 20:45:09.826: ERROR/AndroidRuntime(6541): FATAL EXCEPTION: main
08-23 20:45:09.826: ERROR/AndroidRuntime(6541): java.lang.RuntimeException: Unable to stop service uk.ac.ic.doc.vmw10.wherewolf.helpers.Locater@44f53fa8: java.lang.NullPointerException
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at android.app.ActivityThread.handleStopService(ActivityThread.java:3090)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at android.app.ActivityThread.access$3700(ActivityThread.java:125)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2099)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at android.os.Looper.loop(Looper.java:123)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at java.lang.reflect.Method.invokeNative(Native Method)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at java.lang.reflect.Method.invoke(Method.java:521)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at dalvik.system.NativeStart.main(Native Method)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541): Caused by: java.lang.NullPointerException
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at uk.ac.ic.doc.vmw10.wherewolf.helpers.Locater.onDestroy(Locater.java:137)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     at android.app.ActivityThread.handleStopService(ActivityThread.java:3076)
08-23 20:45:09.826: ERROR/AndroidRuntime(6541):     ... 10 more

If I remove that line, the NullPointerException goes away, but the LocationListener doesn't stop when I close my application. Any ideas how I can fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩心不撩汉 2024-12-08 05:00:35

将其包装在空检查中怎么样?

if (locationManager != null)
    locationManager.removeUpdates(this);

How about wrapping it in a null check?

if (locationManager != null)
    locationManager.removeUpdates(this);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文