简体   繁体   English

带有匕首 2 的 Kotlin 中的匕首缺失绑定错误

[英]Error dagger missingBinding in Kotlin with dagger 2

I'm facing a little issue after i tried to implement Dagger 2 in Koltin, i almost tried everything i saw in other topic on Stack over flow and other articles but nothing worked out for me, this is the error i'm facing, if any one can kindly help or guide me to solve it, thank you在我尝试在 Koltin 中实现 Dagger 2 后,我遇到了一个小问题,我几乎尝试了我在 Stack over flow 和其他文章的其他主题中看到的所有内容,但对我来说没有任何结果,这是我面临的错误,如果任何人都可以帮助或指导我解决它,谢谢

  • This is the error generated这是产生的错误
error: [Dagger/MissingBinding] java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,? extends javax.inject.Provider<androidx.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.
public abstract interface MainComponent extends dagger.android.AndroidInjector<com.example.foodapp.di.BaseApplication> {
                ^
      java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,? extends javax.inject.Provider<androidx.lifecycle.ViewModel>> is injected at
          com.example.foodapp.di.DiComponents.ViewModelProviderFactory(creators)
      com.example.foodapp.di.DiComponents.ViewModelProviderFactory is injected at
          com.example.foodapp.MainActivity.provider
      com.example.foodapp.MainActivity is injected at

  • This is my AuthViewModelModule where i map my viewmodel class这是我的 AuthViewModelModule 我 map 我的视图模型 class
@Module
abstract class AuthViewModelsModule {
    @Binds
    @IntoMap
    @ViewModelKey(MainViewModel::class)
    abstract fun bindAuthViewModel(viewModel: MainViewModel): ViewModel?
}
  • This is my ViewModelKey这是我的 ViewModelKey
@MustBeDocumented
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)
  • This is my ViewModelProvierFactory这是我的 ViewModelProvierFactory
lass ViewModelProviderFactory @Inject constructor(private val creators: 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) { // if the viewmodel has not been created

            // loop through the allowable keys (aka allowed classes with the @ViewModelKey)
            for ((key, value) in creators) {

                // if it's allowed, set the Provider<ViewModel>
                if (modelClass.isAssignableFrom(key)) {
                    creator = value
                    break
                }
            }
        }

        // if this is not one of the allowed keys, throw exception
        requireNotNull(creator) { "unknown model class $modelClass" }

        // return the Provider
        return try {
            creator.get() as T
        } catch (e: Exception) {
            throw RuntimeException(e)
        }
    }

    companion object {
        private const val TAG = "ViewModelProviderFactor"
    }

}

  • This is AppComponent class这是应用组件 class
@Component(modules = [AndroidSupportInjectionModule::class,AndroidBuildersModule::class, ViewModelFactoryModule::class, AppModule::class])
interface MainComponent : AndroidInjector<BaseApplication>{

    @Component.Builder
    interface toBuild
    {
        @BindsInstance
        fun Building(application: Application) : toBuild
        fun build() : MainComponent
    }
}

It looks like you forgot to add AuthViewModelsModule.class to modules in the AppComponent class您似乎忘记将AuthViewModelsModule.class添加到 AppComponent class 中的模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM