Observable OnCompleted无法更新UI

Ami*_*.io 3 java android observable rx-java

我正在尝试Toast完成服务电话.但是在onComplete方法中,我收到此异常:

java.lang.RuntimeException:无法在未调用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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是我的代码中代码片段,问题出现了:

 NetworkService.aService()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread()) 
                    .flatmap(anotherCall)
                    .subscribe(new Subscriber<AddCommentResponse>() {
                @Override
                public void onCompleted() {
                    Toast.makeText(...).show();
                    navigateBack();
                }

                // etc. ...
Run Code Online (Sandbox Code Playgroud)

我无法从onCompleted方法更新UI的问题是什么?或者我应该在哪里处理UI操作?

dwu*_*sen 9

也许anotherCall在另一个线程上观察.调用observeOn后移动(在Android主线程上)flatMap.


Ami*_*.io 5

本来:

NetworkService.aService()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread()) 
                    .flatmap(anotherCall)
Run Code Online (Sandbox Code Playgroud)

在此更改后,它正在工作:

NetworkService.aService()
                    .flatmap(anotherCall)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread()) 
Run Code Online (Sandbox Code Playgroud)