I have an error with hilt, i dont know why. I keep...
# dagger
g
I have an error with hilt, i dont know why. I keep getting the error:
java.lang.RuntimeException: Cannot create an instance of class app.medxpert.pgo.compose.privacy_agreement.PrivacyAgreementViewModel
The Preferences:
Copy code
class Preferences @Inject constructor(
    @ApplicationContext val context: Context
) {...}
My ViewModel:
Copy code
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class PrivacyAgreementViewModel @Inject constructor(
    private val sharedPrefs: Preferences,
) : ViewModel() {...}
How I call it:
Copy code
@Composable
fun PrivacyAgreementView(
    viewModel: PrivacyAgreementViewModel = hiltViewModel()
) {
What could be wrong?
If I comment out the preferences, the code works.
Oh i see:
Caused by: java.lang.InstantiationException: java.lang.Class<app.medxpert.pgo.compose.privacy_agreement.PrivacyAgreementViewModel> has no zero argument constructor
but it is injected?
f
Make sure you annotate your MainActivity with @AndroidEntryPoint too
173 Views