【问题标题】:@dagger.hilt.android.qualifiers.ApplicationContext android.content.Context cannot be provided without an @Provides-annotated method@dagger.hilt.android.qualifiers.ApplicationContext android.content.Context 不能在没有@Provides-annotated 方法的情况下提供
【发布时间】:2020-08-14 23:10:50
【问题描述】:

我正在做一个 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 的新手 - 有什么想法吗?

【问题讨论】:

  • Kristy,你找到解决办法了吗,这个问题快把我逼疯了
  • 这里也一样 - 如果您找到任何解决方案,如果您能分享您的解决方案,那就太好了

标签: android dagger-hilt


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2019-06-16
    • 2023-03-06
    • 1970-01-01
    • 2017-11-05
    • 2019-11-06
    • 2021-05-05
    • 2021-06-11
    • 1970-01-01
    • 2021-12-05
    相关资源
    最近更新 更多