我想在我的代码中使用 courutine,但我收到了这个错误,该错误仅在 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]
这是我的代码,我尝试了很多方法,但仍然错误,不知道如何解决这个问题,仅在 inputHarta 上出现错误,你们能解决我的问题吗
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
}
}
在IO线程中进行UI操作是不正确的。您应该使用如下代码在主线程中执行此类操作:
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
}
总而言之,将 UI 刷新代码放在主线程中,并将耗时的任务(例如读写文件)放在 IO 线程中。