如何解决lateinit属性chargeSessionRepo尚未初始化
在扩展 BroadcasrReceiver 的类中,我想访问存储库函数。 我收到错误lateinit property chargeSessionRepo尚未初始化
。
我已经指定了 @Inject,还需要做什么,这样我就可以访问 ChargeSessionRepository 中的函数
class CustomNotificationListener : BroadcastReceiver() {
@Inject
lateinit var chargeSessionRepo: ChargeSessionRepository
override fun onReceive(context: Context, intent: Intent) {
Log.d("fcmService", "onReceive ${intent.getStringExtra("onClickListener")}")
when(intent.getStringExtra("onClickListener")) {
"approve" -> {
approvePlugin(true)
}
"deny" -> {
approvePlugin(false)
}
}
}
private fun approvePlugin(isApproved: Boolean) {
GlobalScope.launch {
try {
val chargeStatusUpdate = chargeSessionRepo.getChargeStatusUpdate().data
val chargeStatus = chargeStatusUpdate?.firstOrNull()
chargeStatus?.sessionId?.let {
chargeSessionRepo.approvePlugIn(it, isApproved)
}
} catch (exception: Exception) {
Log.d("fcmService", "exception: ${exception}") // lateinit property chargeSessionRepo has not been initialized
}
}
}
}
ChargesessionRepository
@Singleton
class ChargeSessionRepository @Inject constructor(
private val chargeSessionService: ChargeSessionService,
private val chargeSessionDao: ChargeSessionDao
) : Repository() {
var lastChargeStatus: List<ChargeStatusDTO>? = null
private set
suspend fun getChargeStatusUpdate(): Resource<List<ChargeStatusDTO>> {
val resource = getResource { chargeSessionService.getChargeStatus() }
lastChargeStatus = resource.data
return resource
}
}
您能建议如何执行此操作吗?
谢谢 右
in a class which extends BroadcasrReceiver, I want to access a repository function.
I get an error lateinit property chargeSessionRepo has not been initialized
.
I have specified @Inject, what else needs to be done, so I can access the function in ChargeSessionRepository
class CustomNotificationListener : BroadcastReceiver() {
@Inject
lateinit var chargeSessionRepo: ChargeSessionRepository
override fun onReceive(context: Context, intent: Intent) {
Log.d("fcmService", "onReceive ${intent.getStringExtra("onClickListener")}")
when(intent.getStringExtra("onClickListener")) {
"approve" -> {
approvePlugin(true)
}
"deny" -> {
approvePlugin(false)
}
}
}
private fun approvePlugin(isApproved: Boolean) {
GlobalScope.launch {
try {
val chargeStatusUpdate = chargeSessionRepo.getChargeStatusUpdate().data
val chargeStatus = chargeStatusUpdate?.firstOrNull()
chargeStatus?.sessionId?.let {
chargeSessionRepo.approvePlugIn(it, isApproved)
}
} catch (exception: Exception) {
Log.d("fcmService", "exception: ${exception}") // lateinit property chargeSessionRepo has not been initialized
}
}
}
}
ChargesessionRepository
@Singleton
class ChargeSessionRepository @Inject constructor(
private val chargeSessionService: ChargeSessionService,
private val chargeSessionDao: ChargeSessionDao
) : Repository() {
var lastChargeStatus: List<ChargeStatusDTO>? = null
private set
suspend fun getChargeStatusUpdate(): Resource<List<ChargeStatusDTO>> {
val resource = getResource { chargeSessionService.getChargeStatus() }
lastChargeStatus = resource.data
return resource
}
}
could you please suggest how to do this
Thanks
R
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能忘记将
@AndroidEntryPoint
放在广播接收器的顶部。如果您使用刀柄。You may have forgotten to put
@AndroidEntryPoint
on top of your broadcast receiver. if you are using hilt.