Skip to content Skip to sidebar Skip to footer

The Class Cannot Inherit From The Androidviewmodel, Why?

I try create a new class that inherits from AndroidViewModel, like this public class LoginViewModel extends AndroidViewModel { public LoginViewModel() {} ... But I get thi

Solution 1:

You will need to add super call to AndroidViewModel class when inheriting from it. AndroidViewModel class contains default constructor having Application class as variable, so you should change your implementation as below :

publicclassLoginViewModelextendsAndroidViewModel {

   publicLoginViewModel(Application application) {
       super(application);
       // Do rest of your stuff here ...
   }
...

Default implementation of AndroidViewModel class states that :

Application context aware ViewModel.

Subclasses must have a constructor which accepts Application as the only parameter.

Solution 2:

AndroidViewModel has only one public constructor that takes an Application as parameter. You must call this from your constructor:

public LoginViewModel(Application app) {
    super(app);
}

Solution 3:

Post a Comment for "The Class Cannot Inherit From The Androidviewmodel, Why?"