我WorkManager
1.0.0-alpha02
在依赖项中使用 foundandroid.arch.work:work-runtime
每隔一分钟查询一个网页(此示例排除了查询代码,因为它无关紧要)。
当我WorkManager
这样使用时:
WorkManager
.getInstance()
.enqueue(
new PeriodicWorkRequest.Builder(
MessageWorker.class,
1,
TimeUnit.MINUTES
)
.setConstraints(
new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
)
.build()
);
private class MessageWorker extends Worker {
public MessageWorker() {
}
@NonNull
@Override
public WorkerResult doWork() {
// Do stuff
return WorkerResult.SUCCESS;
}
}
它给出了这个错误信息:
05-31 17:50:05.645 11749-12168/com.neelkamath.webview E/WorkerWrapper: Trouble instantiating com.neelkamath.webview.MainActivity$MessageWorker
java.lang.InstantiationException: java.lang.Class<com.neelkamath.webview.MainActivity$MessageWorker> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at androidx.work.impl.WorkerWrapper.workerFromClassName(WorkerWrapper.java:405)
at androidx.work.impl.WorkerWrapper.workerFromWorkSpec(WorkerWrapper.java:377)
at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:132)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
05-31 17:50:05.645 11749-12168/com.neelkamath.webview E/WorkerWrapper: Could for create Worker com.neelkamath.webview.MainActivity$MessageWorker
我尝试了以下构造函数无济于事:
- 没有构造函数
- 上市
- 私人的
- 具有非空主体的构造函数,其唯一语句为
super();