将依赖项注入ViewModel时的Dagger / MissingBinding

问题描述 投票:0回答:1

我正在尝试将我的存储库注入到ViewModels中。但是,在编译代码时,我一直收到此错误。我不确定该去哪里...

C:\Users\Anon\AndroidStudioProjects\Barrechat192\app\build\tmp\kapt3\stubs\debug\com\example\barrechat192\di\AppComponent.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 \ ...

android kotlin dependency-injection dagger android-viewmodel
1个回答
0
投票

您可能不需要片段的子组件。让我为您简化代码...

© www.soinside.com 2019 - 2024. All rights reserved.