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

Android application crashes when I start an 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.access00(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.access00(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 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) {

    }


}

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

这是我用来启动它的代码

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

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

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