Android Viewmodel Has No Zero Argument Constructor

Android ViewModel has no zero argument constructor

While initializing subclasses of ViewModel using ViewModelProviders, by default it expects your UserModel class to have a zero argument constructor.
In your case your constructor has the argument MutableLiveData<User> user.

One way to fix this is to have a default no arg constructor for your UserModel.

Otherwise, if you want to have a non-zero argument constructor for your ViewModel class, you may have to create a custom ViewModelFactory class to initialise your ViewModel instance, which implements the ViewModelProvider.Factory interface.

I have not tried this yet, but here's a link to an excellent Google sample for this: github.com/googlesamples/android-architecture-components.
Specifically, check out this class GithubViewModelFactory.java for Java code and this class GithubViewModelFactory.kt for the corresponding Kotlin code.

Hilt ViewModel has no zero argument constructor

You need to upgrade to Fragment 1.2.0 or higher.

As per the Lifecycle 2.2.0 release notes, the new ViewModelProvider APIs that Hilt uses under the hood only apply when using Fragment 1.2.0 or higher. When using an older version of Fragments, those APIs are not connected to fragments and therefore your Hilt enabled ViewModel factory is not used when you use by viewModels().

You should upgrade to Fragment 1.2.5 (the last version of the Fragment 1.2.X set) or to Fragment 1.3.0, both of which contain the necessary API hooks to get Hilt working.

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

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.

ViewModel has no zero argument constructor error - even when it has a zero argument constructor

Either:

  • Move MyViewModel to a separate Java file, or

  • Make MyViewModel be a static class

Right now, you have defined MyViewModel as an inner class of MainActivity. That will not work, as only an instance of MainActivity can create an instance of MyViewModel. In particular, ViewModelProvider cannot create an instance of MyViewModel.

ViewModel has no zero argument constructor hilt Java

Your error message shows that the HiltViewModelFactory doesn't have a binding for your specific AuthViewModel. It doesn't because you haven't included the additional annotationProcessor that is needed specifically for Hilt and Jetpack integrations - it is that extra androidx.hilt:hilt-compiler annotation processor that is responsible for reading the @ViewModelInject and generating the correct Hilt bindings.

dependencies {
// Hilt
implementation 'com.google.dagger:hilt-android:2.30-alpha'
annotationProcessor 'com.google.dagger:hilt-android-compiler:2.30-alpha'

// Hilt Jetpack Integrations
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02'
annotationProcessor 'androidx.hilt:hilt-compiler:1.0.0-alpha02'
...
}

AndroidViewModel has no zero argument constructor

if you're using hilt, you probably forgot to annotate your activity with @AndroidEntryPoint

ViewModel has no zero argument constructor

Try replacing:

sharedTaskViewModel = ViewModelProvider(this).get(TaskViewModel::class.java)

with

sharedTaskViewModel = ViewModelProvider.AndroidViewModelFactory(application)
.create(TaskViewModel::class.java)


Related Topics



Leave a reply



Submit