https://kotlinlang.org logo
#dagger
Title
# dagger
g

Geert

09/28/2021, 1:02 PM
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

FunkyMuse

09/28/2021, 9:03 PM
Make sure you annotate your MainActivity with @AndroidEntryPoint too
71 Views