如果没有@Provides注释的方法,则无法提供Provider <ViewModel >>

时间:2019-06-15 20:00:35

标签: android kotlin dagger-2

尝试绑定ViewModelProvider.Factory时出现下一个错误:

/.../di/AppComponent.java:8: error: [Dagger/MissingBinding] [dagger.android.AndroidInjector.inject(T)] 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 extends dagger.android.AndroidInjector<com.contedevel.juke.App> {
                ^
      java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> is injected at
          com.....di.ViewModelFactory.<init>(creators)
      com.....di.ViewModelFactory is injected at
          com.....di.ViewModelModule.bindViewModelFactory(factory)
      androidx.lifecycle.ViewModelProvider.Factory is injected at
          com....ui.login.AccountFragment.viewModelFactory
      com.....ui.login.AccountFragment is injected at
          dagger.android.AndroidInjector.inject(T)
  component path: com.....di.AppComponent → com.....di.ActivityModule_ContributeLoginActivity.LoginActivitySubcomponent → com.....di.LoginFragmentBuilderModule_ContributeAccountFragment.AccountFragmentSubcomponent

这是我对工厂的实现:

@Singleton
class ViewModelFactory @Inject constructor(
    private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
): ViewModelProvider.Factory {

    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        val creator = creators[modelClass] ?: creators.entries.firstOrNull {
            modelClass.isAssignableFrom(it.key)
        }?.value ?: throw IllegalArgumentException("unknown model class $modelClass")

        try {
            @Suppress("UNCHECKED_CAST")
            return creator.get() as T
        } catch (e: Exception) {
            throw RuntimeException(e)
        }
    }
}

匕首实体:

@Suppress("unused")
@Module
abstract class ViewModelModule {

    @Binds
    abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
}

@Singleton
@Component(
    modules = [
        AndroidInjectionModule::class,
        AppModule::class,
        ViewModelModule::class,
        ActivityModule::class
    ]
)
interface AppComponent: AndroidInjector<App> {

    @Component.Builder
    abstract class Builder: AndroidInjector.Builder<App>()
}

@Suppress("unused")
@Module
abstract class ActivityModule {
    @ContributesAndroidInjector(
        modules = [LoginFragmentBuilderModule::class, LoginViewModelModule::class]
    )
    abstract fun contributeLoginActivity(): LoginActivity

    @ContributesAndroidInjector(modules = [])
    abstract fun contributeMainActivity(): MainActivity
}

@Suppress("unused")
@Module
abstract class LoginViewModelModule {

    @Binds
    @IntoMap
    @ViewModelKey(AccountViewModel::class)
    abstract fun bindAccountViewModel(accountViewModel: AccountViewModel): ViewModel
}

@Suppress("unused")
@Module
abstract class LoginFragmentBuilderModule {

    @ContributesAndroidInjector
    abstract fun contributeAccountFragment(): AccountFragment
}

我使用那个tutorial书写,但我仍然失败了。有谁知道如何解决?问题出在哪里?

0 个答案:

没有答案