Search code examples
androidandroid-gradle-pluginobservablerx-javarx-java2

How to fix error - cannot resolve method 'subscribe(anonymous rx.functions.Action1<java.lang.boolean>)


I have changed some imports for Rxjava related to AndroidSchedulers and Observable. when I did that I get the following error.

I am not sure how to proceed with it.

Error

enter image description here

These are my changed imports in my presenter.

import io.reactivex.Flowable;
//import rx.schedulers.Schedulers;
import io.reactivex.schedulers.Schedulers;
//import rx.Observable;
import io.reactivex.Observable;
//import rx.android.schedulers.AndroidSchedulers;
import io.reactivex.android.schedulers.AndroidSchedulers;

Error is on this piece of code as shown in the picture

  public void setUserFocusChangeObservable(Observable<Boolean> observable) {
        subscriptions.add(observable
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean hasFocus) {
                        //If screen is dimmed, do not allow interaction
                        if (!screenUtils.isScreenDimmed()) {
                            if (user != null) {
                                if (!hasFocus) {
                                    view.renderUserText(user.toString());
                                }
                            } else {
                                //Set text to nothing if interaction happens with autosuggest when screen is dimmed
                                view.renderUserText("");
                            }
                        }
                    }
                }));
    }

In the picture I have also highlighted my imports from Gradle

your suggestions are very helpful R


Solution

  • RxJava 2 changed several class names from RxJava 1. In particular, Action1 has been changed to Consumer:

                .subscribe(new Consumer<Boolean>() { /* ... */ })
    

    Make sure you import io.reactivex.functions.Consumer instead of java.util.function.Consumer.

    Alternatively, you can use a Java 8 lambda:

                .subscribe(hasFocus -> { /* ... */ })
    

    When the parameter to subscribe is fixed, this will also reveal another class name difference: CompositeSubscription is now CompositeDisposable.