我已经按照
Android Developer’s教程使用工作管理器结构在后台运行我的代码,但是当我尝试将我的工作人员排队时它不会运行,我收到以下错误:
2018-10-04 22:25:47.004 28669-28885/app.package.com.debug E/DefaultWorkerFactory: Could not instantiate app.package.com.MyWorker java.lang.NoSuchMethodException: <init> [] at java.lang.Class.getConstructor0(Class.java:2320) at java.lang.Class.getDeclaredConstructor(Class.java:2166) at androidx.work.DefaultWorkerFactory.createWorker(DefaultWorkerFactory.java:58) at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:180) at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:117) 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) 2018-10-04 22:25:47.005 28669-28885/app.package.com.debug E/WorkerWrapper: Could for create Worker app.package.com.MyWorker
我已经看到这个问题可能与worker上的默认构造函数有关但我已经使用了正确的构造函数而不是不推荐使用的默认函数.
我的工人被宣布为:
public class MyWorker extends Worker { public MyWorker(@NonNull Context context,@NonNull WorkerParameters workerParams) { super(context,workerParams); } @NonNull @Override public Result doWork() { // Doesn't even get called } }
它就像这样排队:
WorkManager workManager = WorkManager.getInstance(); if (myWorkerRequest == null) { myWorkerRequest = new OneTimeWorkRequest.Builder(MyWorker.class) .setConstraints(new Constraints.Builder() .setrequiredNetworkType(NetworkType.CONNECTED) .build()) .build(); } WorkStatus workStatus = workManager.getStatusById(myWorkerRequest.getId()).getValue(); if (workStatus == null || !workStatus.getState().isFinished()) { workManager.enqueue(myWorkerRequest); }
我没有看到与示例有什么不同,所以我想了解还有什么可能会影响我的代码导致此崩溃.它可能与ProGuard有关吗?
我的版本是1.0.0-alpha09
谢谢!
解决方法
这是带有WorkManager 1.0.0-alpha09的
known issue,已经标记为alpha10固定.
-keepclassmembers class * extends androidx.work.Worker { public <init>(android.content.Context,androidx.work.WorkerParameters); }