2

好的,所以我在一个小部件中声明了一个 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在方法之前调用onUpdatejob变量没有以某种方式初始化,我不知道如何进一步修复或调试它

这怎么可能在 onEnabled 中初始化的作业变量实际上没有在 onUpdate 函数中初始化?这是一个错误还是什么?

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

4

1 回答 1

1

这只是吐槽,因为我不知道 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) }
        }
    }
}

“静态”保留对 s 的引用通常是一个坏主意Context,但这可能没问题,因为它们已在onDisabled. 或者也许WeakHashMap改用。

于 2020-05-01T12:01:01.770 回答