当我启动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);