简体   繁体   English

RXJava / RXAndroid-无法在未调用Looper.prepare()的线程内创建处理程序

[英]RXJava/RXAndroid - Can't create handler inside thread that has not called Looper.prepare()

When you received an error with this message 当您收到此消息的错误时

rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError rx.exceptions.OnErrorFailedException:尝试将错误传播到Observer.onError时发生错误

but your subscription is already handling onError 但是您的订阅已经在处理onError

MyMethodThatRetunsAnObservable(string)
        .subscribe(
            response -> handleResponse(response),
            throwable -> handleError(throwable));

In case that is caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 如果是由于以下原因引起的:java.lang.RuntimeException:无法在尚未调用Looper.prepare()的线程内创建处理程序

Note that onError notifications will cut ahead of onNext notifications on the emission thread if Scheduler is truly asynchronous. 请注意,如果Scheduler确实是异步的,则onError通知将在发射线程的onNext通知之前切入。 So it's needed to specify the UI Thread in which we are going to observe 因此需要指定我们将在其中观察的UI线程

public static void shortToast(String msg) {
    Observable.just(msg)
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(message -> {
                Toast.makeText(App.getInstance(), message, Toast.LENGTH_SHORT).show();
            });
}

yes, you got the error. 是的,您遇到了错误。 You should call subscribeOn(AndroidSchedulers.mainThread() instead of observerOn . The show log function was called before the observerOn and called in the onSubcribe method. 您应该调用subscribeOn(AndroidSchedulers.mainThread()而不是observerOn 。,显示日志函数在observerOn之前调用,并在onSubcribe方法中调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RxJava onError无法在尚未调用Looper.prepare()的线程内创建处理程序 - RxJava onError Can't create handler inside thread that has not called Looper.prepare() RxJava无法在未调用Looper.prepare()的线程内创建处理程序 - RxJava Can't create handler inside thread that has not called Looper.prepare() 无法在未调用Looper.prepare()的线程内创建处理程序 - Can not create handler inside thread that has not called Looper.prepare() 无法在Handler()内未调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() inside Handler() Handler或runOnUiThread解决方案“无法在未调用Looper.prepare()的线程内创建处理程序” - Handler or runOnUiThread solution for “Can't create handler inside thread that has not called Looper.prepare() ” Android处理程序:无法在尚未调用Looper.prepare()的线程内创建处理程序 - Android handler: Can't create handler inside thread that has not called Looper.prepare() Android线程错误-无法在未调用Looper.prepare()的线程内创建处理程序 - Android thread error - Can't create handler inside thread that has not called Looper.prepare() 无法在警告对话框线程中未调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() on alert dialog thread 无法在 AsyncTask 内未调用 Looper.prepare() 的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask 无法在未调用Looper.prepare()的线程内创建处理程序 - 对话框中的AsyncTask - Can't create handler inside thread that has not called Looper.prepare() - AsyncTask inside a dialog
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM