控制柄和工作管理器错误:Java.lang.NoSuchMethodException异常:< init>[类android.content.上下文,类androidx.work.WorkerParameters]

z9ju0rcb  于 2023-03-16  发布在  Android
关注(0)|答案(2)|浏览(130)

我正在使用工作管理器与剑柄,但得到以下错误。
我指的是Android教程https://developer.android.com/training/dependency-injection/hilt-jetpack

错误

java.lang.NoSuchMethodException: com.debug.check.TestWorker.<init> [class android.content.Context, class androidx.work.WorkerParameters]
        at java.lang.Class.getConstructor0(Class.java:2332)
        at java.lang.Class.getDeclaredConstructor(Class.java:2170)
        at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:95)
        at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:245)
        at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:137)
        at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
        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:929)

分级版本:

implementation 'androidx.work:work-runtime-ktx:2.7.1'
    // When using Kotlin.
kapt("androidx.hilt:hilt-compiler:1.0.0")
implementation 'androidx.hilt:hilt-work:1.0.0'

测试人员

@HiltWorker
class TestWorker @AssistedInject constructor(
    @Assisted context: Context,
    @Assisted chatParameters: WorkerParameters,
    private val mainRepository: MainRepository
) : CoroutineWorker(context, chatParameters) {

    companion object {
        const val KEY_RESULT = "KEYRESULT"
    }

    override suspend fun doWork(): Result = coroutineScope {
        try {
            println("test")
            mainRepository.refreshMovies()
        } catch (e: Exception) {
            Result.failure()
        }
        Result.success()
    }
}

我的应用程序

@HiltAndroidApp
class MyApplication : Application(), Configuration.Provider {
    @Inject
    lateinit var workerFactory: HiltWorkerFactory

    override fun getWorkManagerConfiguration() =
        Configuration.Builder()
            .setWorkerFactory(workerFactory)
            .build()
    override fun onCreate() {
        super.onCreate()
       
    }
}

主要活动

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        WorkManager workManager = WorkManager.getInstance(this);
        PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(TestChatWorker.class
                , 5, TimeUnit.SECONDS)
                .addTag("test")
                .build();

        workManager.enqueueUniquePeriodicWork("test", ExistingPeriodicWorkPolicy.KEEP, periodicWorkRequest);
        workManager.getWorkInfoByIdLiveData(periodicWorkRequest.getId()).observe(this, new Observer<WorkInfo>() {
            @Override
            public void onChanged(WorkInfo workInfo) {
                if (workInfo != null) {
                    if (workInfo.getState() == WorkInfo.State.SUCCEEDED) {
                        String chatResult = workInfo.getOutputData().getString(TestWorker.KEY_RESULT);

                    }
                }
            }
        });
    }
}
  • 它在初始化部分继续失败。* 我尝试将工作管理器降级到2.6,但仍然无法解决这个问题。我已经在manifest.xml中实现了。
<provider
            android:name="androidx.startup.InitializationProvider"
            android:authorities="${applicationId}.androidx-startup"
            android:exported="false"
            tools:node="merge">
            <!-- If you are using androidx.startup to initialize other components -->
            <meta-data
                android:name="androidx.work.impl.WorkManagerInitializer"
                android:value="androidx.startup"
                tools:node="remove" />
        </provider>
pkbketx9

pkbketx91#

您应该检查两件事:
检查您正在使用的correct provider entry depending on the version,您正在使用AndroidManifest,这一个为我工作:

<provider
    android:name="androidx.startup.InitializationProvider"
    android:authorities="${applicationId}.androidx-startup"
    tools:node="remove">
</provider>

检查您的build.gradle上是否有这两个kapt依赖项(文档中没有提到这一点,这也是我遇到同样问题的原因):

kapt "com.google.dagger:hilt-android-compiler:2.42"
kapt "androidx.hilt:hilt-compiler:1.0.0"
lyfkaqu1

lyfkaqu12#

在我的项目中,我为不同的任务使用了多个工人类。我也遇到了同样的问题,并通过在清单文件中添加android:multiprocess="true"“provider”标记修复了这个问题。

<provider
            android:name="androidx.startup.InitializationProvider"
            android:authorities="${applicationId}.androidx-startup"
            android:multiprocess="true"
            tools:node="remove">
</provider>

相关问题