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

时间:2020-08-14 23:10:50

标签: android dagger-hilt

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

[Dagger / MissingBinding] @ dagger.hilt.android.qualifiers.ApplicationContext android.content.Context没有@Provides注释的方法就无法提供。

@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的新手-有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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

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