EN VI

Android - What is DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@66c20ba, Dispatchers.IO]?

2024-03-15 16:30:05
Android - What is DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@66c20ba, Dispatchers.IO]

I wanna use courutine on my code but i got this error, the error only on HartaActivity

java.lang.RuntimeException: Can't create handler inside thread Thread[DefaultDispatcher-worker-5,5,main] that has not called Looper.prepare()
                                                                                                        at android.os.Handler.<init>(Handler.java:207)
                                                                                                        at android.os.Handler.<init>(Handler.java:119)
                                                                                                        at android.view.textservice.SpellCheckerSession$1.<init>(SpellCheckerSession.java:107)
                                                                                                        at android.view.textservice.SpellCheckerSession.<init>(SpellCheckerSession.java:107)
                                                                                                        at android.view.textservice.TextServicesManager.newSpellCheckerSession(TextServicesManager.java:205)
                                                                                                        at android.widget.SpellChecker.resetSession(SpellChecker.java:129)
                                                                                                        at android.widget.SpellChecker.setLocale(SpellChecker.java:150)
                                                                                                        at android.widget.SpellChecker.spellCheck(SpellChecker.java:225)
                                                                                                        at android.widget.Editor.updateSpellCheckSpans(Editor.java:806)
                                                                                                        at android.widget.Editor.sendOnTextChanged(Editor.java:1377)
                                                                                                        at android.widget.TextView.sendOnTextChanged(TextView.java:10539)
                                                                                                        at android.widget.TextView.setText(TextView.java:6272)
                                                                                                        at android.widget.TextView.setText(TextView.java:6097)
                                                                                                        at android.widget.EditText.setText(EditText.java:122)
                                                                                                        at android.widget.TextView.setText(TextView.java:6049)
                                                                                                        at com.ch.al_mirats.presentation.kalkulator.harta.HartaActivity$displayWarisData$1.invokeSuspend(HartaActivity.kt:134)
                                                                                                        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                                                                                        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
                                                                                                        at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
                                                                                                        at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103)
                                                                                                        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
                                                                                                        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
                                                                                                        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
                                                                                                        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
                                                                                                        Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@66c20ba, Dispatchers.IO]

this is my code, i try many ways but its still error, idk how solve this problem, error only on inputHarta, can u guys solve my problem please

private fun displayWarisData() {
        GlobalScope.launch(Dispatchers.IO) {
            val loadedWarisData = warisDataDao.getAllWarisData()

            var harta = ""
            var hutang = ""
            var wasiat = ""
            var biayaPerawatanJenazah = ""


            for (warisData in loadedWarisData) {
                harta = warisData.harta.toString()
                hutang = warisData.hutang.toString()
                wasiat = warisData.wasiat.toString()
                biayaPerawatanJenazah = warisData.biayaPerawatanJenazah.toString()
            }
            val hartaEditable: Editable = Editable.Factory.getInstance().newEditable(if(harta == "0") "" else harta)
            val hutangEditable: Editable = Editable.Factory.getInstance().newEditable(if(hutang == "0") "" else hutang)
            val wasiatEditable: Editable = Editable.Factory.getInstance().newEditable(if(wasiat == "0") "" else wasiat)
            val biayaPerawatanJenazahEditable: Editable = Editable.Factory.getInstance().newEditable(if(biayaPerawatanJenazah == "0") "" else biayaPerawatanJenazah)

            binding.inputHarta.text = hartaEditable
            binding.inputHutang.text = hutangEditable
            binding.inputWasiat.text = wasiatEditable
            binding.inputBiayaPerawatanJenazah.text = biayaPerawatanJenazahEditable

        }
    }

Solution:

Doing UI operations in IO thread is not correct. You should do such operations in Main thread using some code like this:

withContext(Dispatchers.Main) {
        val hartaEditable: Editable = Editable.Factory.getInstance().newEditable(if (harta == "0") "" else harta)
        val hutangEditable: Editable = Editable.Factory.getInstance().newEditable(if (hutang == "0") "" else hutang)
        val wasiatEditable: Editable = Editable.Factory.getInstance().newEditable(if (wasiat == "0") "" else wasiat)
        val biayaPerawatanJenazahEditable: Editable = Editable.Factory.getInstance().newEditable(if (biayaPerawatanJenazah == "0") "" else biayaPerawatanJenazah)

        binding.inputHarta.text = hartaEditable
        binding.inputHutang.text = hutangEditable
        binding.inputWasiat.text = wasiatEditable
        binding.inputBiayaPerawatanJenazah.text = biayaPerawatanJenazahEditable
    }

In summary, put your UI refresh code in Main thread and put time-consuming task (such as reading and writing files) in IO thread.

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login