gpt4 book ai didi

android - 为什么即使初始化了Lateinit属性,我仍会得到kotlin.UninitializedPropertyAccessException(可能)

转载 作者:行者123 更新时间:2023-12-02 13:30:47 27 4
gpt4 key购买 nike

好的,所以我在如下小部件中声明了lateinit var job

class TempHumidDisplayWidget : AppWidgetProvider(), CoroutineScope {
private lateinit var job: Job
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
...

在onEnabled函数中,我对其进行了初始化:

override fun onEnabled(context: Context) {
job = Job()
Log.i("com.github.animeshz", "On enabled")
...

在onUpdate函数中,我将使用启动函数,该函数调用coroutineContext的get函数来调度使用作业变量的协程。

override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
// There may be multiple widgets active, so update all of them
for (appWidgetId in appWidgetIds) {
launch { updateAppWidget(context, appWidgetManager, appWidgetId) }
}
}

在logcat内部,出现以下错误:

4425-4425/? I/com.github.animeshz: On enabled
...
2020-05-01 11:06:06.639 4425-4425/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.github.animeshz.shivamwidget, PID: 4425
java.lang.RuntimeException: Unable to start receiver com.github.animeshz.shivamwidget.TempHumidDisplayWidget: kotlin.UninitializedPropertyAccessException: lateinit property job has not been initialized
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3183)
at android.app.ActivityThread.-wrap18(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1636)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6334)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property job has not been initialized
at com.github.animeshz.shivamwidget.TempHumidDisplayWidget.getCoroutineContext(TempHumidDisplayWidget.kt:26)
at kotlinx.coroutines.CoroutineContextKt.newCoroutineContext(CoroutineContext.kt:33)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:50)
at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
at com.github.animeshz.shivamwidget.TempHumidDisplayWidget.onUpdate(TempHumidDisplayWidget.kt:38)
at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3171)
at android.app.ActivityThread.-wrap18(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1636) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6334) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

It is clear that onEnabled is called before the onUpdate method but the job variable is not initialized somehow, I don't know how to fix or debug it further



在onEnabled函数中实际上未初始化的在onEnabled中初始化的作业变量怎么可能呢?这是错误还是什么?

任何帮助将不胜感激,在此先感谢!

最佳答案

这只是在吐口水,因为我不知道Android中小部件的详细信息,但是这样的方法可能有效:

class TempHumidDisplayWidget : AppWidgetProvider() {
companion object {
val jobs = mutableMapOf<Context, Job>()

fun getScope(context: Context): CoroutineScope {
val job = jobs[context] ?: throw IllegalStateException("Context $context not enabled currently")
return object : CoroutineScope {
override val coroutineContext: CoroutineContext = Dispatchers.Main + job
...
}
}
}

override fun onEnabled(context: Context) {
jobs[context] = Job()
...
}

override fun onDisabled(context: Context) {
jobs.remove(context)?.cancel()
...
}

override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
val scope = getScope(context)
for (appWidgetId in appWidgetIds) {
scope.launch { updateAppWidget(context, appWidgetManager, appWidgetId) }
}
}
}

通常,“静态地”保留对 Context的引用是一个坏主意,但这可能是正确的,因为在 onDisabled中已将其删除。或改为使用 WeakHashMap

关于android - 为什么即使初始化了Lateinit属性,我仍会得到kotlin.UninitializedPropertyAccessException(可能),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61537595/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com