AndroidViewModel has no zero argument constructor. How to solve this?

Tags: , ,



I’ve searched for my problem nearly whole the Internet and couldn’t find the answer, which I need. Yes. My question is not unique, but there was no answer, which could help me. So I decided to ask it myself.

Now about the problem:

public class AuthActivityViewModel extends AndroidViewModel {

    Repository repository;
    private LiveData<List<User>> users;
    private LiveData<User> user;

    public AuthActivityViewModel(@NonNull Application application) {
        super(application);

        repository = new Repository(application);
    }

    public LiveData<List<User>> getUsers() {
        users = repository.getUsers();
        return users;
    }

    public LiveData<User> getUser(int userId) {
        user = repository.getUser(userId);
        return user;
    }

    public void addNewUser(User user) {
        repository.insertUser(user);
    }

    public void updateUser(User user) {
        repository.updateUser(user);
    }

    public void deleteUser(User user) {
        repository.deleteUser(user);
    }
}

……………………………………..

public class AuthActivity extends AppCompatActivity implements AuthInteractionListener {

    private FragmentManager fragmentManager;
    private final ConnectionBroadcastReceiver connectionBroadcastReceiver = new ConnectionBroadcastReceiver();

    private int limitOfOnWindowFocusChangedOperationForAuthActivity = 0;
    private boolean isActivityRecreated;

    AuthActivityViewModel authActivityViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auth);

        isActivityRecreated = true;
        IntentFilter connectionIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(connectionBroadcastReceiver, connectionIntentFilter);

        authActivityViewModel = new ViewModelProvider(this).get(AuthActivityViewModel.class);
    }
    ...
}

………….

Error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.findthepikachuupgradeversion2/com.example.android.findthepikachuupgradeversion3.view.AuthActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.android.findthepikachuupgradeversion3.viewmodel.AuthActivityViewModel
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3307)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3446)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7548)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
 Caused by: java.lang.RuntimeException: Cannot create an instance of class com.example.android.findthepikachuupgradeversion3.viewmodel.AuthActivityViewModel
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
    at com.example.android.findthepikachuupgradeversion3.view.AuthActivity.onCreate(AuthActivity.java:44)
    at android.app.Activity.performCreate(Activity.java:7893)
    at android.app.Activity.performCreate(Activity.java:7880)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3282)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3446) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:224) 
    at android.app.ActivityThread.main(ActivityThread.java:7548) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
 Caused by: java.lang.InstantiationException: java.lang.Class<com.example.android.findthepikachuupgradeversion3.viewmodel.AuthActivityViewModel> has no zero argument constructor
    at java.lang.Class.newInstance(Native Method)
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187) 
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) 
    at com.example.android.findthepikachuupgradeversion3.view.AuthActivity.onCreate(AuthActivity.java:44) 
    at android.app.Activity.performCreate(Activity.java:7893) 
    at android.app.Activity.performCreate(Activity.java:7880) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3282) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3446) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:224) 
    at android.app.ActivityThread.main(ActivityThread.java:7548) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
  1. I tried to add a constructor with no arguments in class AuthActivityViewModel, but got this error

There is no default constructor available in androidx.lifecycle.AndroidViewModel

  1. Advices like

    are not actual. Why? Cause they were already as they should be, but I have the error which must not be. As the problem exists, there must be something that I didn’t count.

Here is some part of the gradle file:

def lifecycle_version = “2.2.0” implementation “androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version”

implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
def room_version = "2.2.5"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

How can I solve this problem?

Answer

Add the below dependency to gradle app module level.

implementation 'androidx.lifecycle:lifecycle-extensions:$lifecycle_version

Extensions include many libraries including LiveData, So, by adding it, you can get rid of:

implementation 'androidx.lifecycle:lifecycle-livedata:$lifecycle_version

But it appears lifecycle-extensions has been deprecated. Is there another dependency that solves this issue?

That is right it’s been deprecated as of version:2.2.0.

lifecycle-extensions Artifact Deprecation: With the above deprecation of ViewModelProviders.of(), this release marks the deprecation of the last API in lifecycle-extensions and this artifact should now be considered deprecated in its entirety. We strongly recommend depending on the specific Lifecycle artifacts you need (such as lifecycle-service if you’re using LifecycleService and lifecycle-process if you’re using ProcessLifecycleOwner) rather than lifecycle-extensions as there will not be a future 2.3.0 release of lifecycle-extensions.

As quoted by documentation, you can instead use the specific Lifecycle artifacts: And as we discussed in comments the specific lifecycler aritifacts that works was:

implementation "android.arch.lifecycle:runtime:$lifecycle_version

And also keep the other lifecycler dependency artifacts of yours.



Source: stackoverflow