首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果没有@ @dagger.hilt.android.qualifiers.ApplicationContext注解的方法,则不能提供android.content.Context

如果没有@ @dagger.hilt.android.qualifiers.ApplicationContext注解的方法,则不能提供android.content.Context
EN

Stack Overflow用户
提问于 2020-08-15 07:10:50
回答 1查看 2.7K关注 0票数 5

我正在做一个相对简单的Hilt实现。我已经设置好了,我似乎无法绕过这个错误:

如果没有@Provides注释的方法,则无法提供Dagger/MissingBinding @dagger.hilt.android.qualifiers.ApplicationContext android.content.Context。

代码语言:javascript
复制
@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

下面是我的代码:

代码语言:javascript
复制
@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
}

我是希尔特的新手--有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2020-08-15 10:19:45

我认为发生这个问题是因为在InstallIn()中传递了一个片段组件,并且使用单例注释了您的依赖项,就我所知,单例可以应用于ApplicationComponent,所以尝试将@Singleton注释更改为@FragmentScoped注释

代码语言:javascript
复制
@Provides  
@Singleton --> to @FragmentScoped
fun provideFeatureComponentManager(@ApplicationContext context: Context) : FeatureControlManager {
    return FeatureControlManager.getInstance(context)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63421025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档