首页 > 解决方案 > 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() 的线程内创建处理程序

标签: androidkotlin

解决方案


推荐阅读