如何在 FloatingActionButton/Button Click 事件中使用 Coroutine

vip*_*pin 4 android kotlin android-studio kotlin-coroutines

我有一个 FloatingActionButton,单击它时将从网络下载一些 URL。我想从 setOnClickListener 内部启动这个函数,然后让它在后台完成所有处理。

我已经使用 AsyncTask 做了我想做的事情,但后来有人建议使用协程。

活动内部:

findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
           
            val currentChapUrl = "https://www.novel.com/chap1.html"  
            GlobalScope.launch { downloadChapters(applicationContext,currentChapUrl) } 
        }
Run Code Online (Sandbox Code Playgroud)

downloadChapters 函数如下所示:

suspend fun downloadChapters(context : Context, startingChapUrl : String) : String {
   //download chapters.
   //show some toast messages

}
Run Code Online (Sandbox Code Playgroud)

单击按钮时应用程序崩溃。

日志猫:

2020-08-01 17:03:38.559 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:38.559 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.095 6937-6937/com.example.daoreader I/Choreographer: Skipped 32 frames!  The application may be doing too much work on its main thread.
2020-08-01 17:03:39.124 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.125 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.163 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.163 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.208 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.209 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
    
    
    --------- beginning of crash
2020-08-01 17:03:39.211 6937-7086/com.example.daoreader E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
    Process: com.example.daoreader, PID: 6937
    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        at android.os.Handler.<init>(Handler.java:200)
        at android.os.Handler.<init>(Handler.java:114)
        at android.widget.Toast$TN$2.<init>(Toast.java:336)
        at android.widget.Toast$TN.<init>(Toast.java:336)
        at android.widget.Toast.<init>(Toast.java:103)
        at android.widget.Toast.makeText(Toast.java:256)
        at com.example.daoreader.WebviewActivity.downloadChapters(WebviewActivity.kt:65)
        at com.example.daoreader.WebviewActivity$onCreate$1$1.invokeSuspend(WebviewActivity.kt:32)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)
    
    
    --------- beginning of system
2020-08-01 17:03:39.257 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.257 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.307 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.307 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.394 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.394 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.487 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.487 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.565 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.566 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.581 6937-6985/com.example.daoreader D/EGL_emulation: eglMakeCurrent: 0xaa205000: ver 2 0 (tinfo 0xaa2034c0)
2020-08-01 17:03:39.595 6937-6985/com.example.daoreader D/EGL_emulation: eglMakeCurrent: 0xaa205000: ver 2 0 (tinfo 0xaa2034c0)
2020-08-01 17:03:39.658 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.658 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:39.714 6937-6985/com.example.daoreader D/EGL_emulation: eglMakeCurrent: 0xaa205000: ver 2 0 (tinfo 0xaa2034c0)
2020-08-01 17:03:39.727 6937-6985/com.example.daoreader D/OpenGLRenderer: endAllActiveAnimators on 0x9232d500 (RippleDrawable) with handle 0x92021280
2020-08-01 17:03:40.051 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:40.051 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:40.094 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
2020-08-01 17:03:40.094 6937-6985/com.example.daoreader E/eglCodecCommon: glUtilsParamSize: unknow param 0x000085b5
Run Code Online (Sandbox Code Playgroud)

Fra*_*del 6

它正在崩溃,因为您Toast没有在主线程中显示。

GlobalScope.launch默认在另一个线程中运行代码,即Dispatchers.Default.

所以你需要做什么,在显示之前Toast,你应该将线程更改为主线程,如下所示

suspend fun downloadChapters(context : Context, startingChapUrl : String) : String {
   //download chapters.
   
   withContext(Dispatchers.Main) {
       // Show Toast here
   }
}
Run Code Online (Sandbox Code Playgroud)