android - 如何为 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) {
}
推荐阅读
- typescript - typescript 处理不同类型的返回
- sql - 如何在记事本++中的一个大sql行中将特定字符之后的某些字符替换为另一个特定字符
- laravel - 在 laravel 发布请求中找不到 404
- python - Python 3:AttributeError:从 git bash 启动时,“NoneType”对象没有属性“提示”
- android - 我可以使用 OkHTTP 将 JSON 和表单数据添加到发布请求吗?
- r - 根据案例更改数据框中的值
- flutter - 无法无条件访问属性“isEmpty”,因为接收者可以为“null”
- python - 在python中检测给定ECG数据中的PQRST峰
- javascript - 如何在 vue.js 中的两个组件之间切换?
- python - 如何转换使用多个图像资源创建单个可执行文件的 tkinter 脚本