gpt4 book ai didi

android - @dagger.hilt.android.qualifiers.ApplicationContext android.content.Context 不能在没有@Provides-annotated 方法的情况下提供

转载 作者:行者123 更新时间:2023-12-04 11:40:18 24 4
gpt4 key购买 nike

我正在做一个相对论简单的 Hilt 实现。我已经设置好了,我似乎无法解决这个错误:
[Dagger/MissingBinding] @dagger.hilt.android.qualifiers.ApplicationContext android.content.Context 不能在没有@Provides-annotated 方法的情况下提供。

@dagger.hilt.android.qualifiers.ApplicationContext android.content.Context is injected at
com.greendotcorp.core.managers.featurecontrol.app.FeatureControlManager(context, …)
com.greendotcorp.core.managers.featurecontrol.app.FeatureControlManager is injected at
com.greendotcorp.core.features.dashboard.GridDashboardFragment.featureControlManager
com.greendotcorp.core.features.dashboard.GridDashboardFragment is injected at

com.greendotcorp.core.utils_theme.ViewModelDependencyInjector.inject
这是我的代码:
@Singleton
@Component(modules = [ViewModelInjectorModule::class])
interface ViewModelDependencyInjector {
fun inject(fragment: GridDashboardFragment)
}

@InstallIn(FragmentComponent::class)
@Module
object DashboardModule {

@Provides
@Singleton
fun provideFeatureComponentManager(@ApplicationContext context: Context) : FeatureControlManager {
return FeatureControlManager.getInstance(context)
}

@AndroidEntryPoint
class GridDashboardFragment : BaseDetailFragment() {

@Inject lateinit var featureControlManager: FeatureControlManager
}
我是 Hilt 的新手——有什么想法吗?

最佳答案

我认为问题正在发生,因为在 InstallIn() 中,您正在传递一个 fragment 组件,并且您正在使用 singleton 注释您的依赖项,据我所知,可以将单例应用于 ApplicationComponent ,因此请尝试将 @Singleton 注释更改为 @FragmentScoped Annotation

@Provides  
@Singleton --> to @FragmentScoped
fun provideFeatureComponentManager(@ApplicationContext context: Context) : FeatureControlManager {
return FeatureControlManager.getInstance(context)
}

关于android - @dagger.hilt.android.qualifiers.ApplicationContext android.content.Context 不能在没有@Provides-annotated 方法的情况下提供,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63421025/

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