【问题标题】:Problems stopping a LocationListener Service停止 LocationListener 服务的问题
【发布时间】:2011-08-23 20:51:56
【问题描述】:

我有一个实现 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 不会在我关闭应用程序时停止。有什么想法可以解决这个问题吗?

【问题讨论】:

  • 这是您的代码:uk.ac.ic.doc.vmw10.wherewolf.helpers.Locater?或者您是否有权访问源代码?您可以检查该行或将其放在这里。它可能会为我们提供如何帮助您的线索

标签: android location intentservice


【解决方案1】:

把它包装在一个空检查中怎么样?

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

【讨论】:

    猜你喜欢
    • 2013-12-25
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    相关资源
    最近更新 更多