【发布时间】: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