android - android RuntimeException:无法在未调用 Looper.prepare() 的线程内创建处理程序
问题描述
我必须在后台运行一个函数,并且在该函数执行时我必须显示一个进度对话框。这就是我调用函数的方式
.setPositiveButton(getString(R.string.str_accept)) { _, _ ->
CoroutineScope(Dispatchers.Default).launch {
pdfReportRequest()
}
这就是我执行协程任务的方式
private suspend fun pdfReportRequest() {
val alertDialog: AlertDialog?
progressDialogBuilder.setView(progressBinding.root)
.setTitle(context?.getString(R.string.exporting_as_pdf_file))
.show()
withContext(Dispatchers.Default) {
getResultFromApi()
}
alertDialog = progressDialogBuilder.create()
alertDialog.dismiss()
}
但它给了我这个错误
java.lang.RuntimeException:无法在未调用 Looper.prepare() 的线程内创建处理程序
解决方案
推荐阅读
- entity-component-system - 如何在 ECS 游戏中处理重复数据,以及有关 ECS 的更多一般问题
- javascript - 如何编写一个函数,返回一个宽为 5,高为 8 的矩形的周长?
- javascript - 如何在 ReactJS 应用程序中插入第 3 方库
- r - 在数据框R中添加带有条件的递归数
- python - 日期时间pyplot错误TypeError中熊猫系列的错误concat:float()参数必须是字符串或数字,而不是'Timestamp',,
- javascript - 在 Angular 中添加/删除/编辑列表项时自动更新视图
- python - 集成终端VSCode:显示路径但不输出
- sqlite - SQL多对多查询选择用户好友
- wordpress - 从本地迁移到实时 wordpress 需要新安装
- react-native - 是否存在使 Ionicons 有一些新闻反馈的属性?