Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App crashes when closed while using a service android, as im trying to use intents

public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getExtras() != null) {
        String temp = intent.getStringExtra("key");
        temp1 = Integer.parseInt(temp);
        counter = temp1;
    }
}

I'm passing data to the service and it works perfectly when the app is open but crashes as soon as i close the app.. I've tried a lot of things and I'm new to android. Any help would be appreciated

Thanks in advance.

Here is the log

03-21 15:35:07.868 21818-21818/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.cs442.shash5259.assignment_6, PID: 21818
                                                   java.lang.RuntimeException: Unable to start service com.cs442.shash5259.assignment_6.Myservice@9d88461 with null: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                                                       at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3045)
                                                       at android.app.ActivityThread.-wrap17(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1452)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:148)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                                                       at com.cs442.shash5259.assignment_6.Myservice.onStartCommand(Myservice.java:55)
                                                       at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3028)
                                                       at android.app.ActivityThread.-wrap17(ActivityThread.java) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1452) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:148) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5443) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
like image 897
Shashank Shankaranand Avatar asked Dec 08 '25 09:12

Shashank Shankaranand


1 Answers

Looking at the logs, it is clear that your Intent object in onStartCommand is null, when app is closed.

Looking at the documentation inside the source code of Service, it is mentioned that:

The Intent supplied to onStartCommand may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY.

You can deal with this in at least 2 different ways depending on your app's requirement.

1.You can modify the code as below in your onStartCommand:

public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent!= null) {
            String temp = intent.getStringExtra("key");
            temp1 = Integer.parseInt(temp);
            counter = temp1;
        }
    }

This will prevent the crash but will not update the counter

2.Or you may return START_REDELIVER_INTENT in the onStartCommand() callback in service so that the entire intent is sent following a restart, thus preventing Intent from being null.

like image 59
AADProgramming Avatar answered Dec 09 '25 23:12

AADProgramming



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!