Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Android template error Transformation.map

I’ve tryied to use a template (tabbed view activity) in a new project (java).. but it won’t run (no code modifications). Version 2.1.2 on Win 11
error given:

C:\Progetti\Android\Test\app\src\main\java\it\gattoneroph\test\ui\main\PageViewModel.java:12: error: method map in class Transformations cannot be applied to given types;
private LiveData mText = Transformations.map(mIndex, new Function<Integer, String>() {
^
required: LiveData,Function1<X,Y>
found: MutableLiveData,<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,Function1<X,Y>)
Y extends Object declared in method <X,Y>map(LiveData,Function1<X,Y>)

class

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class PageViewModel extends ViewModel {

    private MutableLiveData<Integer> mIndex = new MutableLiveData<>();
    private LiveData<String> mText = Transformations.map(mIndex, new Function<Integer, String>() {
        @Override
        public String apply(Integer input) {
            return "Hello world from section: " + input;
        }
    });

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

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

why? I’ve no idea why this template seems to be broken.

>Solution :

As explained in this known issue, the template is generating the code expected by earlier versions of the Lifecycle library. However, that code changed in Lifecycle 2.6.0.

As your error message says, replace new Function with new Function1, which is the expected class when using Lifecycle 2.6.0 or higher.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading