将依赖项注入ViewModel时的Dagger / MissingBinding
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将依赖项注入ViewModel时的Dagger / MissingBinding相关的知识,希望对你有一定的参考价值。
我正在尝试将我的存储库注入到ViewModels中。但是,在编译代码时,我一直收到此错误。我不确定该去哪里...
C:UsersAnonandroidStudioProjectsBarrechat192appuild mpkapt3stubsdebugcomexamplearrechat192diAppComponent.java:8: 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.
public abstract interface AppComponent {
^
A binding with matching key exists in component: com.example.barrechat192.ui.fragments.barremap.di.BarreMapComponent
A binding with matching key exists in component: com.example.barrechat192.ui.fragments.camera.di.CameraComponent
A binding with matching key exists in component: com.example.barrechat192.ui.fragments.photoview.di.PhotoViewComponent
java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> is injected at
com.example.barrechat192.di.FoundationViewModelFactory(creators)
com.example.barrechat192.di.FoundationViewModelFactory is injected at
com.example.barrechat192.di.ViewModelBuilderModule.bindViewModelFactory(factory)
androidx.lifecycle.ViewModelProvider.Factory is injected at
com.example.barrechat192.ui.fragments.photoeditor.PhotoEditorFragment.viewModelFactory
com.example.barrechat192.ui.fragments.photoeditor.PhotoEditorFragment is injected at
正如它们在示例中所做的那样,我用一个组件设置了我的App,然后为每个片段创建了子组件。
@Singleton
@Component(
modules = [
AppModule::class,
ViewModelBuilderModule::class,
SubcomponentsModule::class
]
)
interface AppComponent {
@Component.Factory
interface Factory {
fun create(@BindsInstance applicationContext: Context) : AppComponent
}
fun barreMapComponent(): BarreMapComponent.Factory
fun cameraComponent() : CameraComponent.Factory
fun photoEditorComponent(): PhotoEditorComponent.Factory
fun photoViewComponent(): PhotoViewComponent.Factory
}
@Module(
subcomponents = [
BarreMapComponent::class,
CameraComponent::class,
PhotoEditorComponent::class,
PhotoViewComponent::class
]
)
object SubcomponentsModule
每个子组件都有一个与其要注入的ViewModel相关的模块。我正在显示四个之一。
@Subcomponent(modules = [BarreMapModule::class])
interface BarreMapComponent {
@Subcomponent.Factory
interface Factory{
fun create() : BarreMapComponent
}
fun inject(fragment: BarreMapFragment)
}
@Module
abstract class BarreMapModule {
@Binds
@IntoMap
@ViewModelKey(BarreMapViewModel::class)
abstract fun bindViewModel(viewModel: BarreMapViewModel) : ViewModel
}
最后是处理ViewModelFactory注入的模块,
class FoundationViewModelFactory @Inject constructor(
private val creators: @JvmSuppressWildcards Map<Class<out ViewModel>, Provider<ViewModel>>
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
var creator: Provider<out ViewModel>? = creators[modelClass]
if (creator == null) {
for ((key, value) in creators) {
if (modelClass.isAssignableFrom(key)) {
creator = value
break
}
}
}
if (creator == null) {
throw IllegalArgumentException("Unknown model class: $modelClass")
}
try {
@Suppress("UNCHECKED_CAST")
return creator.get() as T
} catch (e: Exception) {
throw RuntimeException(e)
}
}
}
@Module
abstract class ViewModelBuilderModule {
@Binds
abstract fun bindViewModelFactory(
factory: FoundationViewModelFactory
): ViewModelProvider.Factory
}
@Target(
AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER
)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
annotation class ViewModelKey(val value: KClass<out ViewModel>)
然后将它们原样注入片段中>
class BarreMapFragment: Fragment() {
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
private val mapViewModel by viewModels<BarreMapViewModel> { viewModelFactory }
}
我正在尝试将我的存储库注入到ViewModels中。但是,在编译代码时,我一直收到此错误。我不确定该在哪里使用... C: Users Anon AndroidStudioProjects ...
答案
您可能不需要片段的子组件。让我为您简化代码...
以上是关于将依赖项注入ViewModel时的Dagger / MissingBinding的主要内容,如果未能解决你的问题,请参考以下文章
Dagger2:无法在 WorkManager 中注入依赖项
如何使用 Xamarin 和 Autofac 将构造函数依赖项注入 ViewModel?