hello everyone, I am currently stuck because Hilt cannot provide my ViewModel. When running tests, the ViewModel gets constructed correctly using a manual constructor call and the tests pass correctly, so I assume its an error with my Hilt configuration.
I am using Compose and the viewModel() method. It crashes at runtime with the following error message:
André Thiele
12/23/2021, 1:45 PM
Caused by: java.lang.InstantiationException: java.lang.Class<app.example.ui.screens.ExampleViewModel> has no zero argument constructor at java.lang.Class.newInstance(Native Method) at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219) ... 199 more
André Thiele
12/23/2021, 1:46 PM
My ViewModel looks like this:
Copy code
@HiltViewModel
class ExampleViewModel @Inject constructor(
private val exampleRepository: ExampleRepository
) : BaseViewModel<Action, State>() {
André Thiele
12/23/2021, 1:46 PM
Copy code
@Composable
fun ExampleScreen(viewModel: ExampleViewModel = viewModel()) {
m
Marek Defeciński
12/23/2021, 2:04 PM
like here they say you should use hiltViewModel extension