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