Android 工作管理器:“无法实例化 Worker" [英] Android Work Manager: "Could not instantiate Worker"

查看:38
本文介绍了Android 工作管理器:“无法实例化 Worker"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照 Android 开发人员教程使用Worker Manager 结构在后台运行我的代码,但任何时候我尝试将我的工人排队时它都不会运行,并且我收到以下错误:

I've followed the Android Developer's tutorial on using the Worker Manager structure to run my code in background but anytime I try to enqueue my worker it doesn't run and I get the following error:

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 上的默认构造函数有关,但我已经使用了正确的构造函数而不是已弃用的默认函数.

I've seen that this problem could be related to the default constructor on the worker but I already use the correct one instead of the deprecated default function.

我的工人被声明为:

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 相关的东西?

I'm not seeing anything different from the examples so I'd like to understand what else could affect my code to cause this crash. Could it be something ProGuard related?

我的版本是1.0.0-alpha09

谢谢!

推荐答案

这是一个已知问题 WorkManager 1.0.0-alpha09 已经被标记为 alpha10 的固定版本.

This is a known issue with WorkManager 1.0.0-alpha09 that is already marked as fixed for alpha10.

作为一种解决方法,您可以将以下几行添加到您的 proguard 配置中:

As a workaround, you can add the following lines to your proguard configuration:

-keepclassmembers class * extends androidx.work.Worker {
    public <init>(android.content.Context,androidx.work.WorkerParameters);
}

这篇关于Android 工作管理器:“无法实例化 Worker"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆