致命异常:DefaultDispatcher-worker-5 无法在未调用 Looper.prepare() 的线程内创建处理程序

问题描述 投票:0回答:1

致命异常:DefaultDispatcher-worker-5 进程:com.shoptheworldonline.android,PID:5088 java.lang.RuntimeException:无法在未调用 Looper.prepare() 的线程 Thread[DefaultDispatcher-worker-5,5,main] 内创建处理程序 在 android.os.Handler.(Handler.java:227) 在 android.os.Handler 处。(Handler.java:129) 在 android.app.Activity。(Activity.java:925) 在 androidx.core.app.ComponentActivity.(ComponentActivity.java:51) 在 androidx.activity.ComponentActivity.(ComponentActivity.java:282) 在 androidx.fragment.app.FragmentActivity。(FragmentActivity.java:108) 在 androidx.appcompat.app.AppCompatActivity.(AppCompatActivity.java:96) 在 com.example.shoptheworld.android.utils.ErrorService.(ErrorService.kt:23) 在 com.example.shoptheworld.android.PaymentActivity.callPaymentMethod(PaymentActivity.kt:413) 在 com.example.shoptheworld.android.PaymentActivity.access$callPaymentMethod(PaymentActivity.kt:60) 在 com.example.shoptheworld.android.PaymentActivity$onCreate$4$1.invokeSuspend(PaymentActivity.kt:239) 在 kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 在 kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108) 在 kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115) 在 kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103) 在 kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely (CoroutineScheduler.kt:584) 在 kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793) 在 kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697) 在 kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684) 抑制:kotlinx.coroutines.internal.DiagnosticCoroutineContextException:[StandaloneCoroutine{Cancelling}@898620e,Dispatchers.IO]

在手机APP中点击支付选项并点击继续时显示上述错误,如何处理?

android kotlin kotlin-coroutines dispatcher
1个回答
0
投票

您可能正在尝试从后台线程执行 UI 操作,这在 Android 中是不允许的。 尝试在主线程上运行 就像科特林中这样

CoroutineScope(Dispatchers.Main).launch { // Your UI code here } 

© www.soinside.com 2019 - 2024. All rights reserved.