2

I am getting this error after closing (and removing from recent app on my device) my app, it occurs everytime.

09-22 23:44:28.503    4021-4021/? I/art﹕ Late-enabling -Xcheck:jni
09-22 23:44:28.584    4021-4021/cz.united121.android.revizori D/cz.united121.android.revizori.App﹕ onCreate
09-22 23:44:28.642    4021-4021/cz.united121.android.revizori D/cz.united121.android.revizori.service.MyUpdatingService﹕ onCreate
09-22 23:44:28.677    4021-4021/cz.united121.android.revizori D/AndroidRuntime﹕ Shutting down VM
09-22 23:44:28.679    4021-4021/cz.united121.android.revizori E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: cz.united121.android.revizori, PID: 4021
    java.lang.RuntimeException: Unable to start service cz.united121.android.revizori.service.MyUpdatingService@3a7bf84e with null: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getAction()' on a null object reference
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2913)
            at android.app.ActivityThread.access$2100(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getAction()' on a null object reference
            at cz.united121.android.revizori.service.MyUpdatingService.onStartCommand(MyUpdatingService.java:94)
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2896)
            at android.app.ActivityThread.access$2100(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

The line (Log.d(TAG, "onStartCommand" + intent.getAction());) which is exception refer is in Service in onStartCommand:

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        Log.d(TAG, "onStartCommand" + intent.getAction());
        if (intent.getAction().equals(SERVICE_FORCE)) {
            LOCATION_APROVAL = true;
            if (mLastKnownPosition != null) {
                mTimeAproving.run();
            }
        } else if (intent.getAction().equals(SERVICE_START)) {
            mUpdatingTimer.scheduleAtFixedRate(mTimeAproving, 0, PERIOD_BETWEEN_UPDATING);
        } else if (intent.getAction().equals(SERVICE_STOP)) {
            LOCATION_APROVAL = false;
            mLastKnownPosition = null;
            mUpdatingTimer.cancel();
            mUpdatingTimer.purge();
        }
        return START_STICKY;
    }

Thank you in advance.

United121
  • 729
  • 1
  • 10
  • 24

3 Answers3

7

Your app and service were killed, and then the service was restarted as you used the START_STICKY flag. On such occasion, intent passed to onStartCommand() would be null. See Reasons that the passed Intent would be NULL in onStartCommand

Depending on your logic you may consider returning START_REDELIVER_INTENT instead.

Community
  • 1
  • 1
headuck
  • 2,763
  • 16
  • 19
  • 1
    I realy need to study service flag to get understanding what happen. Error was elsewhere but thank you for link. I will read this through. – United121 Sep 23 '15 at 07:36
0

I solve my problem with adding stopSelf() in if(intent.getAction().equals(SERVICE_STOP)) case.

United121
  • 729
  • 1
  • 10
  • 24
  • This solves the issue because the service would not be restarted then after quitting the app (which seems not your intention anyway after your app sends `SERVICE_STOP`). – headuck Sep 23 '15 at 09:38
  • @headuck So I can solve this even with START_NOT_STICKY, which mean after killing service it wont start again. Am I right ? – United121 Sep 23 '15 at 12:46
  • 1
    Yes but you should consider what your service should behave after being interrupted before finishing. If you use `START_NOT_STICKY`, the service won't be restarted, but bear in mind that you app and the service might be killed anytime by the system when the user is not actively using the app. If you want your service to be able to restart and resume its work, then you may need `START_STICKY` and implement the resumption logic on receipt of a null intent. If it is ok to start all over, then you may use `START_REDELIVER_INTENT`. – headuck Sep 23 '15 at 13:01
-1

Well, If anyone is using System.exit(0) by mistake when closing the application by overwrridden method onBackpressed then just remove it and simply use finish(). If System.exit(0) is used it will kill foreground service.

  • I don't understand how this answers the question. The question is over two years old and has an accepted answer. What does your answer add to this? – melwil Jan 27 '18 at 18:22
  • Just put System.exit(0) on onBackpressed method and will get your answer. None of the solution will work if you put System.exit instead of finish(). Service can get killed by many reasons and one of the reason is System.exit(0). – badal soyantar Jan 28 '18 at 19:07