当我启动IntentService时,Android应用程序崩溃了

时间:2015-11-01 17:58:19

标签: android intentservice

我启动IntentService后,我的应用程序崩溃了。我正在尝试使intentservice读取加速度计数据。即使屏幕关闭,我打算使用部分唤醒锁来保持打开状态。 因此,intentService从sensorEventListener扩展而来 我在清单中添加了COURSE_LOCATION和FINE_LOCATION权限。 这是我的堆栈跟踪

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)

这是我的ServiceIntent类

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) {

    }


}

我还没有添加任何代码。所以它不应该崩溃吗?

这是我用来启动它的代码

  Intent mServiceIntent = new Intent(getActivity(), SensorIntentService.class);
                getActivity().startService(mServiceIntent);

1 个答案:

答案 0 :(得分:25)

您需要在SensorIntentService中调用super onCreate():

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