简体   繁体   中英

RxJava. Observable.OnSubscribe cannot be resolved to a type

I'm trying to write my first code in RxJava but i've encountered with some error of library import i suppose.

package second.pack;
import io.reactivex.Observable;
public class Main {
    public static void main(String[] args) {
        Observable <String> observable = Observable.create(
// the line below marked as it has an error in Eclipse
/* 7 line */    new Observable.OnSubscribe<String>(){
                    @Override
                    public void call (Subscriber <String> sub){
                      sub.onNext("New Datas");
                      sub.onComplete(); 
                }
            }
    );
}

}

The Error

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Observable.OnSubscribe cannot be resolved to a type
    at second.pack.Main.main(Main.java:7)

在此处输入图片说明

Please, can anybody help me with this error? Thanks a lot!

You are mixing up constructs between 1.x and 2.x. Are you working with an older tutorial? Try this instead:

import io.reactivex.*;

public class ObservableOnSubscribeTest {
    public static void main(String[] args) {
        Observable<String> observable = Observable.create(
                new ObservableOnSubscribe<String>(){
                    @Override
                    public void subscribe(ObservableEmitter<String> sub){
                        sub.onNext("New Datas");
                        sub.onComplete(); 
                    }
                }
            );
    }
}

Are you sure you have the correct version of RxJava on your classpath? It looks like you're attemping to use RxJava 1.x ( io.reactivex.Observable ) but Observable.OnSubscribe was added in RxJava 2.x ( rx.Observable )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM