gpt4 book ai didi

启动 IntentService 时 Android 应用程序崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:53:55 26 4
gpt4 key购买 nike

我的应用程序在启动 IntentService 后立即崩溃。我正在尝试让 intentservice 读取加速度计数据。我打算使用部分唤醒锁,即使在屏幕关闭时也能保持开启状态。所以 intentService 从 sensorEventListener 扩展我在 list 中添加了 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);

最佳答案

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

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

关于启动 IntentService 时 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33465322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com