簡體   English   中英

帶有匕首 2 的 Kotlin 中的匕首缺失綁定錯誤

[英]Error dagger missingBinding in Kotlin with dagger 2

在我嘗試在 Koltin 中實現 Dagger 2 后,我遇到了一個小問題,我幾乎嘗試了我在 Stack over flow 和其他文章的其他主題中看到的所有內容,但對我來說沒有任何結果,這是我面臨的錯誤,如果任何人都可以幫助或指導我解決它,謝謝

  • 這是產生的錯誤
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

  • 這是我的 AuthViewModelModule 我 map 我的視圖模型 class
@Module
abstract class AuthViewModelsModule {
    @Binds
    @IntoMap
    @ViewModelKey(MainViewModel::class)
    abstract fun bindAuthViewModel(viewModel: MainViewModel): ViewModel?
}
  • 這是我的 ViewModelKey
@MustBeDocumented
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)
  • 這是我的 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"
    }

}

  • 這是應用組件 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
    }
}

您似乎忘記將AuthViewModelsModule.class添加到 AppComponent class 中的模塊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM