【问题标题】:java.lang.IllegalStateException: Expected to be called on the main thread but was RxCachedThreadScheduler in Androidjava.lang.IllegalStateException:预期在主线程上调用,但在 Android 中是 RxCachedThreadScheduler
【发布时间】:2021-08-16 10:59:59
【问题描述】:

我在我的 android 应用程序中遇到异常。

 Non-fatal Exception: java.lang.IllegalStateException: Expected to be called on the main thread but was RxCachedThreadScheduler-20
   at com.jakewharton.rxbinding2.internal.Preconditions.checkMainThread(Preconditions.java:35)
   at com.jakewharton.rxbinding2.view.ViewClickObservable.subscribeActual(ViewClickObservable.java:20)
   at io.reactivex.Observable.subscribe(Observable.java:12197)
   at io.reactivex.internal.operators.observable.ObservableThrottleFirstTimed.subscribeActual(ObservableThrottleFirstTimed.java:41)
   at io.reactivex.Observable.subscribe(Observable.java:12197)
   at io.reactivex.internal.operators.observable.ObservableObserveOn.subscribeActual(ObservableObserveOn.java:45)
   at io.reactivex.Observable.subscribe(Observable.java:12197)
   at io.reactivex.Observable.subscribe(Observable.java:12183)
   at io.reactivex.Observable.subscribe(Observable.java:12111)
   at com.test.myapp.helper.MyHelperKt.setThrottledOnClickListener(MyHelper.kt:34)
   at com.test.myapp.helper.MyHelperKt.setThrottledOnClickListener$default(MyHelper.kt:22)
   at com.test.myapp.ui.view.CustomDialog.showGenericErrorModal(CustomDialog.kt:1059)
   at com.test.myapp.MyApplication.onUnavailable(MyApplication.kt:156)
   at com.test.myapp.di.NetworkingModule$CreateUnavailableServiceErrorInterceptor.intercept(NetworkingModule.kt:316)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
   at com.test.myapp.di.NetworkingModule$CreateAuthInterceptor.intercept(NetworkingModule.kt:351)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
   at com.test.myapp.di.NetworkingModule$CreateGemaltoErrorInterceptor.intercept(NetworkingModule.kt:269)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
   at com.test.myapp.di.NetworkingModule$CreateTokenExpirationInterceptor.intercept(NetworkingModule.kt:258)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
   at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184)
   at okhttp3.RealCall.execute(RealCall.kt:66)
   at retrofit2.OkHttpCall.execute(OkHttpCall.java:186)
   at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:45)
   at io.reactivex.Observable.subscribe(Observable.java:12197)
   at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
   at io.reactivex.Observable.subscribe(Observable.java:12197)
   at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
   at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578)
   at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
   at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
   at java.util.concurrent.FutureTask.run(FutureTask.java:266)
   at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
   at java.lang.Thread.run(Thread.java:923)

下面给出的是导致异常的代码 sn-p。即使observeOn作为主线程传递,我也无法理解为什么会发生这个问题。请帮忙。

    @JvmOverloads
    fun View.setThrottledOnClickListener(throttleTime: Long = Constants.DEFAULT_DEBOUNCE, view: (View) -> Unit): Disposable {
    return RxView.clicks(this).throttleFirst(throttleTime, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe({ view(this) }, {
       
        it.printStackTrace()
    })
}

EDIT :当发生 api 回调并显示自定义对话框时会发生这种情况。这里在设置自定义对话框的时候,设置按钮点击中使用该方法。

【问题讨论】:

    标签: android exception reactivex


    【解决方案1】:

    我也找到了问题和解决方案。这是在工作线程而不是主线程中调用的。当我将其更改为主线程时,它起作用了。

    【讨论】:

      【解决方案2】:

      您是否尝试过使用 subscribeOn()?我无法评论,所以在这里回答。

      【讨论】:

      • 我试过这样返回 RxView.clicks(this).throttleFirst(throttleTime, TimeUnit.MILLISECONDS).subscribeOn(AndroidSchedulers.mainThread())。 observeOn(AndroidSchedulers.mainThread()).subscribe({ view(this) }, { it.printStackTrace() }) 但我无法重现此问题。我从生产 Firebase 崩溃日志中获得的这个崩溃日志。所以我不确定这是否会解决问题或会导致其他一些问题。由于我是 Rx 的新手,我希望有人帮助确认这一点。
      • 我认为setThrottledOnClickListener 不会导致该异常。您能否使用 API 回调以及与对话框相关的代码更新您的答案?
      • 添加了完整的登录问题
      猜你喜欢
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 1970-01-01
      • 2017-01-12
      相关资源
      最近更新 更多