我正在尝试 Toast 完成服务调用。但是在 onComplete 方法中我收到了这个异常:
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
它是从 SafeSubscriber#onNext(T args)
抛出的,如下所示:
/**
* Provides the Subscriber with a new item to observe.
* <p>
* The {@code Observable} may call this method 0 or more times.
* <p>
* The {@code Observable} will not call this method again after it calls either {@link #onCompleted} or
* {@link #onError}.
*
* @param args
* the item emitted by the Observable
*/
@Override
public void onNext(T args) {
try {
if (!done) {
actual.onNext(args);
}
} catch (Throwable e) {
// we handle here instead of another method so we don't add stacks to the frame
// which can prevent it from being able to handle StackOverflow
Exceptions.throwIfFatal(e);
// handle errors if the onNext implementation fails, not just if the Observable fails
onError(e);
}
}
这是我的代码中出现问题的代码 fragment :
NetworkService.aService()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.flatmap(anotherCall)
.subscribe(new Subscriber<AddCommentResponse>() {
@Override
public void onCompleted() {
Toast.makeText(...).show();
navigateBack();
}
// etc. ...
是我无法通过 onCompleted 方法更新 UI 的问题吗?或者我应该在哪里处理 UI 操作?
最佳答案
也许 anotherCall
在另一个线程上观察。在 flatMap
调用之后移动 observeOn
(在 android 主线程上)。
关于java - Observable OnCompleted 无法更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29460569/