Dagger/MissingBinding java.util.Map<java.lang.Class<?扩

Dagger/MissingBinding java.util.Maplt;java.lang.Classlt;? extends ViewModelgt;,Providerlt;ViewModelgt;gt; cannot be provided without an @Provides-annotated method(Dagger/MissingBinding java.util.Mapjava.lang.Class?扩展 ViewModelgt;,Provider
本文介绍了Dagger/MissingBinding java.util.Map<java.lang.Class<?扩展 ViewModel&gt;,Provider&lt;ViewModel&gt;&gt;没有@Provides-annotated 方法就不能提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

这就是我尝试提供 ViewModelFactory 的方式:

This is how I'm trying to provide my ViewModelFactory:

@Suppress("UNCHECKED_CAST")
@Singleton
class ViewModelFactory @Inject constructor(
    private val viewModels: MutableMap<Class<out ViewModel>, Provider<ViewModel>>
) : ViewModelProvider.Factory {

    override fun <T : ViewModel?> create(modelClass: Class<T>): T = viewModels[modelClass]?.get() as T
}

@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@kotlin.annotation.Retention(AnnotationRetention.RUNTIME)
@MapKey
annotation class ViewModelKey(val value: KClass<out ViewModel>)

这就是我绑定 ViewModelFactory 的方式:

And this is how I'm binding the ViewModelFactory:

@Suppress("unused")
@Module
abstract class ViewModelModule  {
    @Binds
    internal abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory

    @Binds
    @IntoMap
    @ViewModelKey(MainViewModel::class)
    internal abstract fun mainViewModel(viewModel: MainViewModel): ViewModel
}

我在构建过程中收到以下错误:

I'm receiving the following error during build:

di/Injector.java:9: error: [Dagger/MissingBinding] java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.

从我的 Activity 我试图以这种方式接收 ViewModelFactory:

From my Activity I'm trying to receive the ViewModelFactory this way:

@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory

推荐答案

在挖掘了一点之后,我发现了这个问题.它与我正在使用的代码完全无关.它涉及 Kotlin 1.3.30.

After digging a little bit more I found the issue. It's completely un-related to the code I'm using. It regards Kotlin 1.3.30.

这里有关它的更多信息.

降级到 Kotlin 1.3.21 解决了这个问题.

Downgrading to Kotlin 1.3.21 resolved the problem.

这篇关于Dagger/MissingBinding java.util.Map<java.lang.Class<?扩展 ViewModel&gt;,Provider&lt;ViewModel&gt;&gt;没有@Provides-annotated 方法就不能提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How To Create a Rotating Wheel Control?(如何创建转轮控件?)
How to avoid restarting activity when orientation changes on Android(如何在 Android 上的方向更改时避免重新启动活动)
Screen orientation lock(屏幕方向锁定)
Strange behavior with android orientation sensor(android方向传感器的奇怪行为)
Android: Rotate image in imageview by an angle(Android:将imageview中的图像旋转一个角度)
Activity restart on rotation Android(旋转Android上的活动重启)