2011-08-23 2 views
1

J'ai un IntentService qui implémente LocationListener. Il s'exécute pendant que mon application est en cours d'exécution et est détruit lorsque mon application est fermée. Dans sa méthode onHandleIntent, j'obtiens un LocationManager et j'enregistre les mises à jour de l'emplacement du formulaire de service. Dans sa méthode OnDestroy, je l'appelle:Problèmes lors de l'arrêt d'un service LocationListener

locationManager.removeUpdates(this); 

Cependant, cela provoque parfois un 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 [email protected]: 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 

Si je supprime cette ligne, le NullPointerException va, mais le LocationListener ne s'arrête pas quand je ferme mon application. Des idées comment je peux résoudre ce problème?

+0

Afficher du code? – darma

+0

Est-ce votre code: uk.ac.ic.doc.vmw10.wherewolf.helpers.Locater? Ou avez-vous accès au code source? Vous pouvez vérifier cette ligne ou le mettre ici. Il pourrait nous donner un indice sur la façon de vous aider – momo

Répondre

2

Que diriez-vous d'envelopper dans une vérification nulle?

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

Ah merci - je n'étais pas tout à fait sûr de ce qui était nul. Tout fixé maintenant – Pikaling

Questions connexes