1

I have to run a function in the background and while the function is performing I have to show a progress dialog. this is how I call the function

.setPositiveButton(getString(R.string.str_accept)) { _, _ ->
                    CoroutineScope(Dispatchers.Default).launch {
                        pdfReportRequest()
                    }

and this is how I perform Coroutine task

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()
    }

but it gives me this error

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

Dhia Shalabi
  • 1,332
  • 1
  • 13
  • 29
  • I have tried to use this `val handler = Handler() handler.post { // Do stuff... progressDialogBuilder.setView(progressBinding.root) .setTitle(context?.getString(R.string.exporting_as_pdf_file)) .show() }` but it does work – Dhia Shalabi Mar 08 '21 at 10:06
  • 1
    U should using `Dispatchers.Main` instead of `Dispatchers.Default` to show Alert dialog . – ADM Mar 08 '21 at 10:17
  • But it gives me another error here ` progressDialogBuilder.setView(progressBinding.root) .setTitle(context?.getString(R.string.exporting_as_pdf_file)) .show() //here` **java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.** – Dhia Shalabi Mar 08 '21 at 10:35
  • This is something else . Probably because you are using same builder again and again i.e . Create a new every time . – ADM Mar 08 '21 at 10:36
  • Ok thank you I got it. – Dhia Shalabi Mar 08 '21 at 10:38

0 Answers0