我有一个FloatingActionButton,点击它会从网上下载一些URL。 我想从setOnClickListener内部启动这个函数,然后让它在后台做所有的处理。
我已经使用AsyncTask做了我想做的事情,但是后来有人建议使用协同任务。
activity内部:
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
val currentChapUrl = "https://www.novel.com/chap1.html"
GlobalScope.launch { downloadChapters(applicationContext,currentChapUrl) }
}
downloadChapters函数如下所示:
suspend fun downloadChapters(context : Context, startingChapUrl : String) : String {
//download chapters.
//show some toast messages
}
该应用程序在点击按钮时崩溃。
日志CAT:
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
它正在崩溃,因为您显示的toast
不在主线程中。
GlobalScope.launch
默认地在另一个线程Dispatchers.Default
中运行代码。
所以您需要做的是,在显示toast
之前,您应该将线程更改为主线程,如下所示
suspend fun downloadChapters(context : Context, startingChapUrl : String) : String {
//download chapters.
withContext(Dispatchers.Main) {
// Show Toast here
}
}
避免使用GlobalScope
。 您应该使用LifeCycleScope
在视图中启动一个协同关系。
您需要首先将androidx.lifecycle:lifecycle-runtime-ktx:{version}
添加到您的gradle文件中。
假设您想在祝酒词中显示downloadchapses
返回的内容,那么我会这样做:
activity内部:
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
lifecycleScope.launch {
val message = downloadChapters(this@MyActivity, "https://www.novel.com/chap1.html")
//Show toast here
}
}
碎片内部:
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
viewLifecycleOwner.lifecycleScope.launch {
val message = downloadChapters(context, "https://www.novel.com/chap1.html")
//Show toast here
}
}
由于LifeCycleScope.launch
在主线程上运行,因此必须在DownloadChapses
期间将其上下文更改为Dispatchers.io
,因此您的方法应该如下所示:
suspend fun downloadChapters(context : Context, startingChapUrl : String) : String = withContext(Dispatchers.IO) {
//download chapters.
"This is my message" //Return your message here
}
在DownloadChapters
内无法更新视图,除非在此之前移动到Dispatchers.Main
,否则将引发异常。 我宁愿在调用那个方法之后再做。