在RxAndroid中创建Observer时出错

时间:2017-09-27 21:15:04

标签: android rx-java rx-android

我正在使用RxAndroid做一个例子,如下面的代码所示。但是Android studio会出现以下错误:

incompatible types
required: io.reactivex.observable <String>
found: void

请告诉我如何解决此错误

Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
        @Override
        public void subscribe(ObservableEmitter<String> e) throws Exception {
            e.onNext("onNext");
            e.onComplete();

        }
    }).subscribe(new Observer<String>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(String value) {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onComplete() {

        }
    });

1 个答案:

答案 0 :(得分:0)

将create和subscribe拆分为不同的语句:

Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
    @Override
    public void subscribe(ObservableEmitter<String> e) throws Exception {
        e.onNext("onNext");
        e.onComplete();

    }
});

observable.subscribe(new Observer<String>() {
    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(String value) {

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public void onComplete() {

    }
});