为什么我得到 kotlin.UninitializedPropertyAccessException 即使 lateinit 属性 已初始化(可能)

Why am i getting kotlin.UninitializedPropertyAccessException even if lateinit property is initialized (probably)

好的,所以我在下面的小部件中声明了一个 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 函数中,我将使用 launch 调用 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中初始化的job变量实际上并没有在onUpdate函数中初始化?这是错误还是什么?

任何帮助将不胜感激,提前致谢!

这只是胡说八道,因为我不知道 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 的 "statically" 的引用通常不是一个好主意,但这可能没问题,因为它们已在 onDisabled 中删除。或者可以改用 WeakHashMap