简体   繁体   English

RxJava。 Observable.OnSubscribe无法解析为一种类型

[英]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. 我正在尝试用RxJava写我的第一个代码,但是我遇到了一些库导入错误。

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. 您正在混合1.x和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? 您确定您的类路径上具有正确的RxJava版本吗? 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 ) 看起来您正在尝试使用RxJava 1.x( io.reactivex.Observable ),但是Observable.OnSubscribe已添加到RxJava 2.x( rx.Observable )中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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