IntentService 中的 Ormlite

Ormlite in a IntentService

我正在使用 OrmLite,但是当我尝试将它用于 IntentService 时出现异常:

E/AndroidRuntime: FATAL EXCEPTION: main Process: fr.solutis.solutis, PID: 29456 java.lang.RuntimeException: Unable to instantiate service fr.solutis.solutis.notifications.NotificationDemandeService: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.app.ActivityThread.handleCreateService(ActivityThread.java:3148) at android.app.ActivityThread.access00(ActivityThread.java:177) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1513) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5910) 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:1405) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.content.ContextWrapper.getResources(ContextWrapper.java:86) at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.openFileId(OrmLiteSqliteOpenHelper.java:310) at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.(OrmLiteSqliteOpenHelper.java:76) at fr.solutis.solutis.helpers.DatabaseHelper.(DatabaseHelper.java:56) at fr.solutis.solutis.notifications.NotificationDemandeService.(NotificationDemandeService.java:25) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.Class.newInstance(Class.java:1690) at android.app.ActivityThread.handleCreateService(ActivityThread.java:3145) at android.app.ActivityThread.access00(ActivityThread.java:177)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1513)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5910)  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:1405)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

代码:

public class NotificationDemandeService extends IntentService {
    DatabaseHelper db = new DatabaseHelper(NotificationDemandeService.this);
    Boolean isInternetPresent = false;
    Boolean isUrlAvailable = false;
    ConnectionDetector cd;
    String message = "";

    public NotificationDemandeService() {
        super("EnvoieService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        try {

            List<Demandes> demandes = db.getAllDemandesRenvoie();
...

        } catch (SQLException e) {
            e.printStackTrace();
        }
....
    }
}

只需致电:

    DatabaseHelper db = new DatabaseHelper(this);

之后:

@Override
protected void onHandleIntent(Intent intent)

行得通,我不明白这个问题,但行得通!