Domanda

I have an app that runs two separate services. One of them runs smoothly, and the other works without issue a majority of the time, but is throwing a NullPointerException within onStartCommand() intermittently on a significant number of newer devices (Samsung, HTC, LG) on varying Android versions between 4.1 and 4.4.2. I am unable to reproduce the error on any of my own devices, including two of the exact models which have experienced this issue. The stacktrace is as follows, indicating the problem is on line 145:

java.lang.RuntimeException: Unable to start service com.mycom.myapp.MyLocService@425ab718
with Intent { cmp=com.mycom.myapp/.MyLocService }: java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2695)
at android.app.ActivityThread.access$1900(ActivityThread.java:146)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5168)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mycom.myapp.MyLocService.onStartCommand(MyLocService.java:145)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2678)
... 10 more


I am using a portion of the code linked within the answer provided here by blackcj to run a location service in the background of my app; here is the relevant snippet from MyLocService.java, with the offending line marked:

@Override
public void onCreate() {
    super.onCreate();

    mInProgress = false;
    mLocationRequest = LocationRequest.create();

    servicesAvailable = servicesConnected();
    mLocationClient = new LocationClient(this, this, this);
    locIntent = new Intent(MY_LOCATION);
}

public int onStartCommand (Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);      // <----- NPE AT LINE 145

    setUpLocationClientIfNeeded();

    if(!mLocationClient.isConnected() || !mLocationClient.isConnecting()) {
        mLocationClient.connect();
    }

    return START_STICKY;
}

private void setUpLocationClientIfNeeded() {
    if(mLocationClient == null) {
            mLocationClient = new LocationClient(this, this, this);
    }
}


The service is started by the following code within onCreate() in MyActivity.java:

Intent MyLocService = new Intent(this, MyLocService.class);     
startService(MyLocService);


The service is declared within the <application> element in AndroidManifest.xml as follows:

    <service
         android:name=".MyLocService"
         android:singleUser="true"
         android:stopWithTask="false" >
         <intent-filter>
             <action android:name="com.mycom.myapp.MyLocService" />
         </intent-filter>
     </service>


I have been unable to determine what is causing this exception, as it does not happen consistently and I cannot reproduce it myself. What could be causing this intermittent problem?

È stato utile?

Soluzione

It appears that returning START_REDELIVER_INTENT instead of START_STICKY has resolved this problem. Instead of continuing to figure out how to check for a null intent, I found a method that supposedly will not cause it in the first place.

I will update with any further conclusions I discover regarding this method as it becomes more widely implemented by my users.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top