java - Observable OnCompleted 无法更新 UI

标签 java android observable rx-java

我正在尝试 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/

相关文章:

java - 没有明显原因的可选数据异常

android - 显示指标未将壁纸缩放至屏幕尺寸

android - 当 state_pressed = true 时更改 <selector> 中的文本大小

f# - 如何从 F# 中的事件设置多个可观察对象?

javascript - 异步管道可观察覆盖闪烁 DOM

java - Play Framework 2.0 - asJson() 总是返回 null

java - 创建具有预定长度的数组的 ArrayList

javascript - 从 Angular 2 中 Chartist 的 API 获取数据

java - Spring Security CSRF token 不适用于 AJAX

Android AppWidget 映射 Activity 问题