gpt4 book ai didi

android - Kotlin dagger 2 Android ViewModel注入(inject)错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:43:43 25 4
gpt4 key购买 nike

我正在尝试在我的 Android 应用程序上使用 dagger 2 从 arch android 库中注入(inject)新的 ViewModel。

从我在这个样本上看到的 https://github.com/googlesamples/android-architecture-components/tree/e33782ba54ebe87f7e21e03542230695bc893818/GithubBrowserSample我需要使用这个:

@MustBeDocumented
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)

@Module
abstract class ViewModelModule {
@Binds
@IntoMap
@ViewModelKey(LoginViewModel::class)
internal abstract fun bindLoginViewModel(viewModel: LoginViewModel): LoginViewModel

@Binds
@IntoMap
@ViewModelKey(MainMenuViewModel::class)
internal abstract fun bindSearchViewModel(viewModel: MainMenuViewModel): MainMenuViewModel

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

@ApplicationScope
@Component(modules = arrayOf(ApplicationModule::class, NetworkModule::class, ViewModelModule::class))
interface ApplicationComponent {
fun plusActivityComponent(activityModule: ActivityModule): ActivityComponent
fun inject(application: LISAApplication)

}

我的工厂是:

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

@Suppress("UNCHECKED_CAST")
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 {
return creator.get() as T
} catch (e: Exception) {
throw RuntimeException(e)
}

}
}

但该项目无法编译 :( 我遇到此错误(如果没有 @Provides-annotated 方法,则无法提供 Map<...>。):

Using Kotlin incremental compilation
:mobile:transformDataBindingWithDataBindingMergeArtifactsForDebug UP-TO-DATE
:mobile:kaptDebugKotlin
e: /Users/jaumard/LISAProjects/LISA/mobile/build/tmp/kapt3/stubs/debug/com/mylisabox/lisa/dagger/components/ApplicationComponent.java:6: error: [com.mylisabox.lisa.dagger.components.ActivityComponent.inject(com.mylisabox.lisa.common.BaseActivity)] java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,javax.inject.Provider<android.arch.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.
e:

e: public abstract interface ApplicationComponent {
e: ^
e: java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,javax.inject.Provider<android.arch.lifecycle.ViewModel>> is injected at
e: com.mylisabox.lisa.dagger.ViewModelFactory.<init>(creators)
e: com.mylisabox.lisa.dagger.ViewModelFactory is injected at
e: com.mylisabox.lisa.common.BaseActivity.factory
e: com.mylisabox.lisa.common.BaseActivity is injected at
e: com.mylisabox.lisa.dagger.components.ActivityComponent.inject(activity)
e: /Users/jaumard/LISAProjects/LISA/mobile/build/tmp/kapt3/stubs/debug/com/mylisabox/lisa/dagger/components/ActivityComponent.java:4: error: com.mylisabox.lisa.dagger.components.ActivityComponent scoped with @com.mylisabox.network.dagger.annotations.ActivityScope may not reference bindings with different scopes:
e:

知道如何解决这个问题吗?

最佳答案

好吧,我发现了问题,问题出在我的 ViewModelModule 下,我需要从我的抽象方法返回 ViewModel,而不是直接返回我想要的类型。那么它会变成这样:

@Module
abstract class ViewModelModule {
@Binds
@IntoMap
@ViewModelKey(LoginViewModel::class)
internal abstract fun bindLoginViewModel(viewModel: LoginViewModel): ViewModel

@Binds
@IntoMap
@ViewModelKey(MainMenuViewModel::class)
internal abstract fun bindSearchViewModel(viewModel: MainMenuViewModel): ViewModel

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

关于android - Kotlin dagger 2 Android ViewModel注入(inject)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46435424/

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