gpt4 book ai didi

android - 尝试使用 Dagger2 和 Kotlin 将 ViewModelProvider 注入(inject) Activity 时出错

转载 作者:太空狗 更新时间:2023-10-29 15:40:13 28 4
gpt4 key购买 nike

我在尝试将 ViewModelProvider.Factory 注入(inject) 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)

我已经创建了非常标准的 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)
}
}
}

我的 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)
}
}

这是 MainViewModel 类:

class MainViewModel @Inject constructor(private val disposable: CompositeDisposable) : ViewModel() {
}

最后是 App 类:

class VolleyballStatsApp : DaggerApplication() {

override fun applicationInjector() = DaggerAppComponent.builder()
.application(this)
.build()
}

我一直在关注 Google 存储库中的 Android Architecture Components Sample,我认为一切似乎都与该实现相似,所以我不明白为什么这不起作用。

当我尝试将 Application 注入(inject) MainAcitvity 时,它可以正常工作,所以问题肯定出在 ViewModelProvider.Factory 上,但不幸的是我不知道问题出在哪里。

最佳答案

我的猜测是您错过了 AndroidSupportInjectionModule。将它添加到 AppComponent 中的模块列表中。我不能真的说出来,因为你没有发布你的 BaseActivity。

引用: https://github.com/google/dagger/issues/783 .

关于android - 尝试使用 Dagger2 和 Kotlin 将 ViewModelProvider 注入(inject) Activity 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47145490/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com