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 :
Applicationcontext awareViewModel.Subclasses must have a constructor which accepts
Applicationas 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:
https://developer.android.com/reference/android/arch/lifecycle/AndroidViewModel
Subclasses must have a constructor which accepts Application as the only parameter.
Post a Comment for "The Class Cannot Inherit From The Androidviewmodel, Why?"