Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android application crashes when I start an IntentService

My application crashes as soon as I start an IntentService. I'm trying to make the intentservice read accelerometer data. I intend to use a partial-wake lock to keep it on even when the screen is off. So the intentService extends from sensorEventListener I've added the COURSE_LOCATION and FINE_LOCATION permissions in the manifest. here's my stack trace

java.lang.RuntimeException: Unable to start service com.shreeasish.safer.SensorIntentService@163c49fe with Intent { cmp=com.shreeasish.safer/.SensorIntentService }: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Message android.app.IntentService$ServiceHandler.obtainMessage()' on a null object reference
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2941)
            at android.app.ActivityThread.access$2100(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1415)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            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:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Message android.app.IntentService$ServiceHandler.obtainMessage()' on a null object reference
            at android.app.IntentService.onStart(IntentService.java:116)
            at android.app.IntentService.onStartCommand(IntentService.java:130)
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2924)
            at android.app.ActivityThread.access$2100(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1415)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            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:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

And here's my ServiceIntent class

public class SensorIntentService extends IntentService  {

    private Sensor mAccelerometer;
    private SensorManager mSensorManager;
//    public static final  String BROADCAST_ACTION = "com.shreeasish.Safer.SERVICE_BROADCAST";
//    public static final String ACCELEROMETER_DATA = "com.shreeasish.Safer.ACCELEROMETER_DATA";

    @Override
    public void onCreate() {

   }



    public SensorIntentService(String name) {
        super(name);
    }

    public SensorIntentService() {
        super("com.shreeasish.Safer");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

    }

//    @Override
    public void onSensorChanged(SensorEvent event) {

    }

//    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }


}

I haven't added any code yet. So it shouldn't crash should it?

This is the code I'm using to launch it

  Intent mServiceIntent = new Intent(getActivity(), SensorIntentService.class);
                getActivity().startService(mServiceIntent);
like image 705
user3927312 Avatar asked Nov 01 '15 17:11

user3927312


People also ask

What is IntentService in Android?

IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent, in turn, using a worker thread, and stops itself when it runs out of work.

Does IntentService Use main thread?

If the task doesn't require any and also not a very long task you can use service. If the Background task is to be performed for a long time we can use the intent service. Service will always run on the main thread. intent service always runs on the worker thread triggered from the main thread.

What is IntentService in Android with example?

The IntentService is used to perform a certain task in the background. Once done, the instance of IntentService terminates itself automatically. An example for its usage would be downloading certain resources from the internet.


1 Answers

You need to call the super onCreate() in your SensorIntentService:

@Override
public void onCreate() {
    super.onCreate();
}
like image 163
Carl Whalley Avatar answered Oct 22 '22 11:10

Carl Whalley