[英]Error while trying to inject ViewModelProvider into Activity with Dagger2 and Kotlin
I'm receiving following error while trying to inject ViewModelProvider.Factory
into Activity
: 尝试将ViewModelProvider.Factory
注入Activity
时,我收到以下错误:
e: D:\AndroidStudioProjects\VolleyballStats\app\build\tmp\kapt3\stubs\debug\com\example\kamil\volleyballstats\di\AppComponent.java:6: error: [dagger.android.AndroidInjector.inject(T)] java.util.Map<java.lang.Class<? extends android.support.v4.app.Fragment>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.support.v4.app.Fragment>>> cannot be provided without an @Provides-annotated method.
e:
e: public abstract interface AppComponent extends dagger.android.AndroidInjector<com.example.kamil.volleyballstats.VolleyballStatsApp> {
e: ^
e: java.util.Map<java.lang.Class<? extends android.support.v4.app.Fragment>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.support.v4.app.Fragment>>> is injected at
e: dagger.android.DispatchingAndroidInjector.<init>(injectorFactories)
e: dagger.android.DispatchingAndroidInjector<android.support.v4.app.Fragment> is injected at
e: dagger.android.support.DaggerAppCompatActivity.supportFragmentInjector
e: com.example.kamil.volleyballstats.views.main.MainActivity is injected at
e: dagger.android.AndroidInjector.inject(arg0)
I've created pretty standard Dagger2 implementation, which looks like this: 我已经创建了非常标准的Dagger2实现,如下所示:
@Singleton
@Component(modules = arrayOf(
AndroidInjectionModule::class,
AppModule::class,
MainActivityModule::class))
interface AppComponent : AndroidInjector<VolleyballStatsApp> {
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: VolleyballStatsApp): Builder
fun build(): AppComponent
}
override fun inject(app: VolleyballStatsApp)
}
@Module
abstract class ViewModelModule {
@Binds
@IntoMap
@ViewModelKey(MainViewModel::class)
abstract fun bindMainViewModel(viewModel: MainViewModel): ViewModel
@Binds
abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
}
@Module(includes = arrayOf(ViewModelModule::class))
class AppModule {
@Provides
fun provideDisposable(): CompositeDisposable {
return CompositeDisposable()
}
}
@Module
internal abstract class MainActivityModule {
@ContributesAndroidInjector(modules = arrayOf(FragmentBuilders::class))
internal abstract fun contributeMainActivity(): MainActivity
}
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@MapKey
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)
class ViewModelFactory @Inject constructor(private val creators: @JvmSuppressWildcards Map<Class<out ViewModel>, @JvmSuppressWildcards 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)
}
}
}
My MainActivity
is defined as following: 我的MainActivity
定义如下:
class MainActivity : BaseActivity() {
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
private lateinit var viewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProviders.of(this, viewModelFactory).get(MainViewModel::class.java)
}
}
Here is MainViewModel
class: 这是MainViewModel
类:
class MainViewModel @Inject constructor(private val disposable: CompositeDisposable) : ViewModel() {
}
And finally App
class: 最后App
类:
class VolleyballStatsApp : DaggerApplication() {
override fun applicationInjector() = DaggerAppComponent.builder()
.application(this)
.build()
}
I've been following Android Architecture Components Sample from Google repository and I think everything seems to be similar to that implementation, so I don't get why this is not working. 我一直在关注来自Google存储库的Android架构组件示例,我认为一切似乎与该实现类似,所以我不明白为什么这不起作用。
When I'm trying to inject Application
to MainAcitvity
then it's working, so the problem is definitely with ViewModelProvider.Factory
, but unfortunately I don't know where the problem is. 当我试图将Application
注入MainAcitvity
然后它正在工作,所以问题肯定是ViewModelProvider.Factory
,但不幸的是我不知道问题出在哪里。
My guess is that you missed the AndroidSupportInjectionModule
. 我的猜测是你错过了AndroidSupportInjectionModule
。 Add it to the list of modules in your AppComponent
. 将其添加到AppComponent
中的模块列表中。 I can't really tell because you did not post your BaseActivity. 我真的不能告诉,因为你不发表您的BaseActivity。
Reference: https://github.com/google/dagger/issues/783 . 参考: https : //github.com/google/dagger/issues/783 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.