Cannot resolve constructor ViewModelProvider(... .FragmentActivity, ... .ui.viewmodels.profileViewModelFactory)

545 Views Asked by At

I'm getting the following error when I hover over the red squiggly line where I'm passing in a custom ViewModelFactory into my ViewModelProvider:

red squiggle of death

Cannot resolve constructor ViewModelProvider(androidx.fragment.app.FragmentActivity, myproject.ui.viewmodels.profileViewModelFactory)

The profileViewModelFactory code is here:

public class profileViewModelFactory extends ViewModelProvider.NewInstanceFactory {
    @NonNull
    private final Application m_Application;
    private final profileRepository m_newProfileRepo;

    public profileViewModelFactory(@NonNull Application application){
        m_Application = application;
        m_newProfileRepo = ((myapp) application).getNewProfileRepository();
    }

    @SuppressWarnings("unchecked")
    @Override
    @NonNull
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        @NonNull T result;
        if (modelClass.isAssignableFrom(profileViewModel.class)) {
            result = (T) new profileViewModel(m_Application, m_newProfileRepo);
        } else {
            throw new IllegalArgumentException("Unknown ViewModel class");
        }
        return result;
    }
}

The strange thing is that this works fine. No compilation errors whatsoever. It just bugs me that Android Studio flags it at all. Incidentally, it recommends I cast the second parameter to 'androidx.lifecycle.ViewModelProvider.Factory' which does cause the red squiggle to go away only to be replaced by a yellow highlight which states

"Casting 'factory' to 'ViewModelProvider.Factory' will produce 'ClassCastException' for any non-null value" 

Which seems to be moving in the wrong direction. If I remember correctly, the error appeared after I made some changes to the gradle. Here's the androidx part:

...

/* androidx  */
implementation 'androidx.fragment:fragment:1.5.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel:2.5.0'
...

Per my research, I did find this and this on SO which are almost my issue (or maybe they are and I'm just not seeing how).

My guess is that I've got more implementations than I need, but I'm not sure which of those (if any) are causing conflicts. Thanks to any who can shine light on this, and I'm happy to add more detail if necessary.

1

There are 1 best solutions below

0
On BEST ANSWER

Generally, you shouldn't be extending ViewModelProvider.NewInstanceFactory, but directly extending the base class, ViewModelProvider.Factory. In this case, NewInstanceFactory isn't providing anything for you (as you don't call super), so the first thing you should try is changing what class you extend to be ViewModelProvider.Factory.