ViewModel中的Kotliin匕首字段注入引发Dagger/Binding异常 [英] Kotliin Dagger Field Injection in ViewModel Throws Dagger/Binding Exception

查看:97
本文介绍了ViewModel中的Kotliin匕首字段注入引发Dagger/Binding异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照本教程进行操作,在我的视图模型中做DI.但是我目前陷入困境.

I have followed this tutorial in order to do DI in my viewmodels. But I currently am stuck.

我为我的viewmodel创建了一个ViewModelFactory,如下所示:

I have created a ViewModelFactory for my viewmodel which is as follows:

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

    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return creators[modelClass]?.get() as T
    }
}

随后是ViewModel:

Followed by a ViewModel:

class HomeViewModel @Inject constructor(private val songsRepository: SongsRepository): ViewModel()

对于DI,我创建了两个组件.一个是我的主要应用程序组件,另一个是依赖于主要应用程序的组件.

For DI I have created two components. One is my main application component, the other is a component which depends on the main application.

@Singleton
@Component(modules = [AppModule::class])
public interface AppComponent {
    fun songRepository(): SongsRepository
    fun libraryManager(): LibraryManager
    fun inject(mainActivity: MainActivity)
}


@Module
public class AppModule(val application: Application){

    @Provides @Singleton
    fun providesApplication(): Application{
        return application
    }

    @Provides @Singleton
    fun providesLibraryManager(): LibraryManager {
        return LibraryManager(application)
    }

    @Provides @Singleton
    fun providesSongRepository(libraryManager: LibraryManager): SongsRepository {
        return SongsRepository(libraryManager)
    }
}

我的ViewModelModule如下:

And my ViewModelModule is as follows:

@Module
public class ViewModelModule {

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

    @AppScope
    @Provides
    fun providesHomeViewModelFactory(providerMap: Map<Class<out ViewModel>, Provider<ViewModel>>): HomeViewModelFactory {
        return HomeViewModelFactory(providerMap)
    }

    @AppScope
    @IntoMap
    @Provides
    @ViewModelKey(HomeViewModel::class)
    fun providesHomeViewModel(songsRepository: SongsRepository): HomeViewModel{
        return HomeViewModel(songsRepository)
    }
}

@AppScope
@Component(modules = [ViewModelModule::class], dependencies = [AppComponent::class])
public interface ViewModelComponent {
    fun homeViewModelFactory(): HomeViewModelFactory
    fun homeViewModel(): HomeViewModel
    fun inject(homeFragment: HomeFragment)
}

我得到的错误是:

错误:[Dagger/MissingBinding] java.util.Map ,?延伸 javax.inject.Provider>不能为 无需使用@Provides注释的方法即可提供.

error: [Dagger/MissingBinding] java.util.Map,? extends javax.inject.Provider> cannot be provided without an @Provides-annotated method.

我真的不知道为什么会这样,因为我所有的类都有@Inject构造函数.匕首的文档也没有帮助我.如果您能在这件事上给我建议,我将不胜感激.

I seriously don't have any idea why is this happening since all my classes have @Inject constructors. Dagger's documentation is not helping me either. I would be grateful if you could advise me on this matter.

推荐答案

错误消息表明以下代码是错误的:

The error message indicates that the following code is wrong:

fun providesHomeViewModelFactory(providerMap: Map<Class<out ViewModel>, Provider<ViewModel>>): HomeViewModelFactory {
    return HomeViewModelFactory(providerMap)
}

应该是

fun providesHomeViewModelFactory(providerMap: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>): HomeViewModelFactory {
    return HomeViewModelFactory(providerMap)
}

这是因为Map接口的签名是Map<K, out V>,这意味着Map<..., Provider<ViewModel>>将被编译为Map<..., ? extends Provider<ViewModel>> Java代码,因此您向dagger询问后一者,但它的前者中只有前者.对象图,然后编译器将引发错误.

It is because the signature of Map interface is Map<K, out V>, that means the Map<..., Provider<ViewModel>> will be compiled to Map<..., ? extends Provider<ViewModel>> Java code, so you are asking dagger for latter one but it only has former one in its object graph, then the compiler throws you the error.

这篇关于ViewModel中的Kotliin匕首字段注入引发Dagger/Binding异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆