好的,所以我在一个小部件中声明了一个 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)
很明显,
onEnabled
在方法之前调用onUpdate
但job
变量没有以某种方式初始化,我不知道如何进一步修复或调试它
这怎么可能在 onEnabled 中初始化的作业变量实际上没有在 onUpdate 函数中初始化?这是一个错误还是什么?
任何帮助将不胜感激,在此先感谢!