我正在按照这篇文章学习RxJava:http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/
第一个示例使用RxJava打印字符串。我做了一点改动。请看下面的代码:
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();
}
}
让它显示敬酒
首先,Android Studio生成的代码是Observable中的call(Object o)和Subscriber
中的onNext(Object o)然后我跟着文章改了,把"Object o"改成"Subscriber o"
但是Android Studio在订户0通知我"Class 'Anonymous class derived from OnSubscribe' must be either declared abstract or implement abstract method 'call(T)' in Action1
",在onNext()通知我"method does not override method from it's superclass
"
我做错了什么?
从您得到的错误来看,Subscriber<? super String> o
似乎不是调用方法所期望的前参数。使用<String>
作为类型应该修复它
Observable<String> myObservable = Observable.create(
new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> o) {
o.onNext("hello world");
o.onCompleted();
}
}
);