所以最近我正在尝试测试 WorkManager 并且我遇到了一个我不知道如何解决它的错误......
我遇到的错误是这个:
2018-06-16 16:36:36.594 23798-23815/? E/AndroidRuntime: FATAL EXCEPTION: pool-2-thread-1
Process: com.example.damiii.myjobintentservice, PID: 23798
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at androidx.room.RoomOpenHelper.checkIdentity(RoomOpenHelper.java:135)
at androidx.room.RoomOpenHelper.onOpen(RoomOpenHelper.java:116)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onOpen(FrameworkSQLiteOpenHelper.java:150)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:409)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:298)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:97)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:55)
at androidx.room.RoomDatabase.beginTransaction(RoomDatabase.java:253)
at androidx.work.impl.utils.EnqueueRunnable.addToDatabase(EnqueueRunnable.java:93)
at androidx.work.impl.utils.EnqueueRunnable.run(EnqueueRunnable.java:79)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
我检查了可能出现此错误的位置,似乎当我对该代码添加注释时:
//WorkRequest can be customised for backoff criteria
OneTimeWorkRequest checkOfflineNetworkState = new OneTimeWorkRequest.Builder(NoNetworkWorker.class).setConstraints(mConstraints).build();
WorkManager.getInstance().enqueue(checkOfflineNetworkState);
我的应用程序运行良好......我不知道问题是来自WorkManager
还是我所做的实现......
这是我的项目的信息:
minSDKversion : 14
targetSDKversion : 28
我的gradle.build
:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
// implementation "com.android.support:appcompat-v7:28.0.0"
// implementation 'com.android.support:support-v4:28.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0-alpha3'
implementation "android.arch.work:work-runtime:1.0.0-alpha02"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
}
有人帮我吗?谢谢你 :)
PS:这是来自的代码NoNetworkWorker
:
package com.example.damiii.myjobintentservice;
import android.util.Log;
import androidx.work.Worker;
public class NoNetworkWorker extends Worker {
/**
* Override this method to do your actual background processing.
*
* @return The result of the work, corresponding to a {@link WorkerResult} value. If a
* different value is returned, the result shall be defaulted to
* {@link WorkerResult#FAILURE}.
*/
@Override
public WorkerResult doWork() {
Log.d("Hello", "I'm offline :D");
return WorkerResult.SUCCESS;
}
}