Arp*_*kla 5 android dependency-injection kotlin dagger-2 dagger-hilt
我试图使用字段注入在我的片段中注入一个类,当我尝试在onViewCreated()
. 我怎样才能解决这个问题?
我试图注入的类:
@Singleton
class DialogHandler @Inject constructor() {
private var currentDialogKey = "dialog_key"
//....
//....
}
Run Code Online (Sandbox Code Playgroud)
我的片段:
@AndroidEntryPoint
class AccountFragment : Fragment(R.layout.fragment_account) {
@Inject
lateinit var dialogHandler: DialogHandler
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val currentDialogKey = dialogHandler.currentDialogKey // throws error saying dialogHandler is uninitialized
//....
//....
}
}
Run Code Online (Sandbox Code Playgroud)
Hilt 已在应用程序中正确设置,并且在其他类中的构造函数注入中工作正常。但由于片段无法实现构造函数注入,因此我尝试使用字段注入并收到此错误。
请帮忙。
编辑:此异常并非每次都会发生。因此,这似乎是一种竞争条件的情况,有时onViewCreated()
在注入之前调用,有时在注入之后调用。
归档时间: |
|
查看次数: |
2934 次 |
最近记录: |