Ezio Shiki
Ezio Shiki

Reputation: 765

Why this simple RxJava example cannot run?

I am learning RxJava following this article:http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/

The first example use RxJava to print a string. I made a little change on it. See the following code.

Observable myObservable = Observable.create(
        new Observable.OnSubscribe() {
            @Override
            public void call(Subscriber<? super String> o) {
                o.onNext("hello world");
                o.onCompleted();
            }
        }
);

Subscriber mySubscriber = new Subscriber() {
    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public void onNext(String s) {
        Toast.makeText(mActivity,s,Toast.LENGTH_SHORT).show();
    }
}

Just make it show a toast.

At first , Android Studio's generated code is call(Object o) in Observable, and onNext(Object o) in Subscriber

Then I changed it followed the article, change "Object o" to "Subscriber o"

But Android Studio notify me " Class 'Anonymous class derived from OnSubscribe' must be either declared abstract or implement abstract method 'call(T)' in Action1 " at Subscriber o. And "method does not override method from it's superclass" at onNext()

What I did wrong?

Upvotes: 2

Views: 1157

Answers (1)

Blackbelt
Blackbelt

Reputation: 157437

From the error you get, it looks like that Subscriber<? super String> o is not the former parameter expected by the call method. Using <String> as type should fix it

Observable<String> myObservable = Observable.create(
        new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> o) {
                o.onNext("hello world");
                o.onCompleted();
            }
        }
);

Upvotes: 2

Related Questions