Observer class must be declared abstract or implem

2019-09-04 11:22发布

I want to use RxAndroid and RxJava in my project. But as shown in the below code, in getAnimalsObserver() method, the following line:

return new Observer<String>

is underscored with a red line saying: //class must be either declared abstract or implement abstract method please let me know how to fix that error.

code: import rx.Observable; import rx.Observer; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers;

//class must be either declared abstract or implement abstract method
private Observer<String> getAnimalsObserver() {
    return new Observer<String>() {
        @Override
        public void onSubscribe(Disposable d) {
            Log.d(TAG, "onSubscribe");
        }

        @Override
        public void onNext(String s) {
            Log.d(TAG, "Name: " + s);
        }

        @Override
        public void onError(Throwable e) {
            Log.e(TAG, "onError: " + e.getMessage());
        }

        @Override
        public void onComplete() {
            Log.d(TAG, "All items are emitted!");
        }
    };
}

1条回答
Luminary・发光体
2楼-- · 2019-09-04 12:13

This is the Observer interface you are implementing:

public interface Observer<T> {

    void onCompleted();

    void onError(Throwable e);

    void onNext(T t);

}

As you can see, it's onCompleted() not onComplete().

Tip: With the cursor inside the body of your implementation class, press Ctrl+I, Android Studio IDE will show dialog with available class members to implement.

查看更多
登录 后发表回答