[英]How to provide Context with Hilt Android?
我正在尝试将项目迁移到 Hilt,但遇到以下问题,不确定如何使用 Hilt 传递上下文。 如果我删除provideContext
方法,它会抱怨以下错误:
error: [Dagger/MissingBinding] @dagger.hilt.android.qualifiers.ApplicationContext android.content.Context cannot be provided without an @Provides-annotated method.
但我的理解是,在 Hilt 中我们不需要provideContext
方法,我们可以像下面这样使用@ApplicationContext
:
@Inject
public CardLayoutManager(@ApplicationContext Context context) {
mContext = context;
}
我错过了什么吗?
您需要正确注释构造函数:
class CardLayoutManager @Inject constructor(@ApplicationContext val context: Context) {
}
删除@ApplicationContext 注解
我已经阅读了所有文档超过 5 遍,尝试了正确的方法并更新了所有 Hilt 库,但问题仍然存在。
一旦我删除了注释,它就完美地工作了,这很奇怪,因为我在我的其他项目中使用了正确的方法(使用 @ApplicationContext 注释)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.