欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Kotlin协程导致的RuntimeException

程序员文章站 2023-12-29 15:21:16
在测试的时候发现协程报出来的问题2020-12-10 17:50:58.877 3078-3078/com.hyfontstudio.fontspro E/AndroidRuntime: FATAL EXCEPTION: main Process: com.hyfontstudio.fontspro, PID: 3078 java.lang.RuntimeException: Unable to stop service com.hyfontstudio.fontspro.ime.core...

在测试的时候发现协程报出来的问题

2020-12-10 17:50:58.877 3078-3078/com.hyfontstudio.fontspro E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.hyfontstudio.fontspro, PID: 3078
    java.lang.RuntimeException: Unable to stop service com.hyfontstudio.fontspro.ime.core.FontsProKeyboard@68e0797: kotlinx.coroutines.JobCancellationException: BlockingCoroutine was cancelled; job=BlockingCoroutine{Cancelled}@b94644b
        at android.app.ActivityThread.handleStopService(ActivityThread.java:4377)
        at android.app.ActivityThread.access$1900(ActivityThread.java:269)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2047)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7860)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
     Caused by: kotlinx.coroutines.JobCancellationException: BlockingCoroutine was cancelled; job=BlockingCoroutine{Cancelled}@b94644b

一直断点排查发现执行完在

    /**
 * Cancels all coroutines and cleans up
 */
override fun onDestroy() {
    runBlocking {
        cancel()
    }
}

出现的问题,才发现一开始声明的

   private val mainScope = MainScope()

应该这样写

     /**
     * Cancels all coroutines and cleans up
     */
    override fun onDestroy() {
        runBlocking {
            mainScope.cancel()
        }
    }

本文地址:https://blog.csdn.net/AAAndroid/article/details/110958774

上一篇:

下一篇: