使用WorkManager入队时出现IllegalAccessException
作者:互联网
尝试使WorkManager 1.0稳定版入队时出现IllegalAccessException.这是堆栈跟踪:
E/WM-WorkerFactory: Could not instantiate com.pocket.sdk.util.service.BackgroundSync$SyncJob
java.lang.IllegalAccessException: java.lang.Class<com.example.BackgroundManager$BackgroundWorker> is not accessible from java.lang.Class<androidx.work.WorkerFactory>
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:92)
at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:233)
at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:127)
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)
worker是一个内部类,声明如下:
private static class BackgroundWorker extends Worker {
// ...
}
如何解决此异常?
解决方法:
看起来WorkManager正在使用反射创建工作程序的实例.但是,因为它被声明为私有内部类,所以尝试执行此操作的WorkerFactory类无法访问.
我所做的就是简单地将我的工人声明为公共内部类,如下所示:
public static class BackgroundWorker extends Worker {
// ...
}
我还确保构造函数是公共的.
但是我很想找到一种解决方案,不需要将工人暴露于全世界.
标签:android-workmanager,android 来源: https://codeday.me/bug/20191024/1922966.html