问题描述
这就是我尝试提供 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>,Provider<ViewModel>>没有@Provides-annotated 方法就不能提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)