[英]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.