Dagger/MissingBinding java.util.Map<java.lang.Class<?扩展 ViewModel&gt;,Provider&lt;ViewModel&gt;&gt;没有@Provides-annotated 方法就不能提供 [英] Dagger/MissingBinding java.util.Map&lt;java.lang.Class&lt;? extends ViewModel&gt;,Provider&lt;ViewModel&gt;&gt; cannot be provided without an @Provides-annotated method

查看:30
本文介绍了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 方法就不能提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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