gpt4 book ai didi

android - 如何在 FloatingActionButton/Button Click 事件中使用协程

转载 作者:行者123 更新时间:2023-12-02 13:06:57 28 4
gpt4 key购买 nike

我有一个 FloatingActionButton,单击它会从 Web 下载几个 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

}

应用在点击按钮时崩溃。

日志:

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之前,你应该像这样将线程更改为Main Thread

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

withContext(Dispatchers.Main) {
// Show Toast here
}
}

关于android - 如何在 FloatingActionButton/Button Click 事件中使用协程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63204706/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com