HX_unbanned
HX_unbanned

Reputation: 597

Transform.map() error in Android when updating several different dependencies

Using latest AS version on Win 10.

When updating any of following dependencies to the latest currently available version:

    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
    implementation 'androidx.core:core:1.12.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'

and using this code for PageView2:

public class PageViewModel extends ViewModel {

    private final MutableLiveData<Integer> mIndex = new MutableLiveData<>();
    private final LiveData<String> mText = Transformations.map(mIndex, new Function<Integer, String>() {
        @Override
        public String apply(Integer input) {
            String text = "";
            String prefixurl = "some text";
            if (input == 1) {
                text = System.lineSeparator() + System.lineSeparator() + "some text" +
                       System.lineSeparator() + "some text" +
                       System.lineSeparator() + "some text";
            } else if (input == 2) {
                text = System.lineSeparator() + System.lineSeparator() + "some text" +
                       System.lineSeparator() + "some text" +
                       System.lineSeparator() + "some text";
            } else if (input == 3) {
                text = System.lineSeparator() + System.lineSeparator() + "some text" +
                       System.lineSeparator() + "some text" +
                       System.lineSeparator() + "some text" +
                       System.lineSeparator() + "some text";
            }
            return "some text: " + input + text + System.lineSeparator() + System.lineSeparator() + "some text.";
        }
    });

    public void setIndex(int index) {
        mIndex.setValue(index);
    }

    public LiveData<String> getText() {
        return mText;
    }

I got the following error:

private final LiveData<String> mText = Transformations.map(mIndex, new Function<Integer, String>() {
                                                          ^
  required: LiveData<X>,Function1<X,Y>
  found:    MutableLiveData<Integer>,<anonymous Function<Integer,String>>
  reason: cannot infer type-variable(s) X,Y
    (argument mismatch; <anonymous Function<Integer,String>> cannot be converted to Function1<X,Y>)
  where X,Y are type-variables:
    X extends Object declared in method <X,Y>map(LiveData<X>,Function1<X,Y>)
    Y extends Object declared in method <X,Y>map(LiveData<X>,Function1<X,Y>)
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

I am not daily nor professional Java programmer, so any noob and dummy level explanation would be very grateful and welcome. I want to fix the code or have the replacement, that works (compiles, builds, runs) with latest dependencies on emulator and physical device.

Let me know of there is some missing or needed info to understand/answer the question.

Upvotes: 0

Views: 26

Answers (0)

Related Questions