如何解决lateinit属性chargeSessionRepo尚未初始化

发布于 2025-01-17 01:18:37 字数 2013 浏览 2 评论 0原文

在扩展 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沒落の蓅哖 2025-01-24 01:18:37

您可能忘记将 @AndroidEntryPoint 放在广播接收器的顶部。如果您使用刀柄。

You may have forgotten to put @AndroidEntryPointon top of your broadcast receiver. if you are using hilt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文