Cannot create an instance of ViewModel class (Unable to start activity ComponentInfo)

Quoting the documentation for AndroidViewModel:

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

Your constructor does not meet that requirement.

Either:

  • Remove the Context context and LifecycleOwner lifecycleOwner constructor parameters from your HomeViewModel, or

  • Create a ViewModelProvider.Factory that can build your HomeViewModel instances, and use that factory with ViewModelProviders.of()


If you are working with Kotlin and you need to inject a dependency inside your ViewModel constructor to work with like a Repository to get data (the same way we use to do with the Presenter layer) you will need to do the following.

Lets say we have a ViewModel that needs a UseCase/Interactor to be injected in the constructor to get data from.

class MainViewModel(private val itemList:ItemListUseCase):ViewModel() {
...
}

When we try to instantiate this ViewModel in our Activity/Fragment, we tend to do this

Fragment example

   class MainFragment : Fragment() {
    private lateinit var mainViewModel: MainViewModel
    
      override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            mainViewModel = requireActivity().run {
                ViewModelProvider(this).get(MainViewModel::class.java)
            }
          ...
        }
}

When we try to run our code, it will crash with a RuntimeException

java.lang.RuntimeException: Cannot create an instance of class com.gaston.example.viewmodel.MainViewModel ... Caused by: java.lang.InstantiationException: java.lang.Class<com.gaston.example.viewmodel.MainViewModel> has no zero argument constructor

This is because we are not injecting the ItemListUseCase when we instantiate our ViewModel

The first thing that comes up is to try to inject it directly from the .get() method of ViewModelProvider

ViewModelProvider(this).get(MainViewModel(ItemListUseCase()))

But if we do this, we will be getting the same error too.

Solution

What we need to understand is that

ViewModelProvider(this).get(MainViewModel::class.java)

ViewModelProvider() is trying to do an instance of the MainViewModel without knowing that it needs a dependency inside its constructor.

To fix this issue, we will need to let ViewModelProvier() know about which dependency we want to inject.

To do this, we need to create a class that will ensure that the instance of that ViewModel needs a dependency to be instantiated.

This class is called a Factory ViewModel class, since it will construct our ViewModel with the dependency it needs to work and then it will let the ViewModelProvier() know which dependency we need to pass to .get(MainViewModel::class.java)

class ViewModelFactory(val requestItemData: ItemListUseCase):ViewModelProvider.Factory {

    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return modelClass.getConstructor(ItemListUseCase::class.java).newInstance(requestItemData)
    }
}

Now, we can tell to ViewModelProviers.of() that it needs an instance of ItemListUseCase::class.java to instantiate MainViewModel(ItemListuseCase())

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mainViewModel = requireActivity().run {
            ViewModelProvider(this,ViewModelFactory(ItemListUseCase())).get(MainViewModel::class.java)
        }

    }

Note that I do not pass any arguments to .get(MainViewModel::class.java) because our Factory will take care of injecting those arguments into our constructor.

At the end, if you want to avoid the Factory class, you can always use Dagger to inject your dependencies without worrying about the ViewModel Factory class.