Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

but your subscription is already handling 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()

Note that onError notifications will cut ahead of onNext notifications on the emission thread if Scheduler is truly asynchronous. So it's needed to specify the UI Thread in which we are going to observe

public static void shortToast(String msg) {
    Observable.just(msg)
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe(message -> {
                Toast.makeText(App.getInstance(), message, Toast.LENGTH_SHORT).show();
            });
}
like image 385
Mara Jimenez Avatar asked Feb 18 '26 04:02

Mara Jimenez


1 Answers

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.

like image 92
Kingfisher Phuoc Avatar answered Feb 19 '26 18:02

Kingfisher Phuoc