[英]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.