skip to Main Content

the program doesn’t run because of run time exceptions such as :

Cannot create an instance of class ViewModel
and has no zero argument constructor
at java.lang.Class.newInstance(Native Method)

i used Inject constuctor like this:

CharactersViewModel @Inject constructor(
private val charactersUseCase : CharactersUseCase,
private val searchCharacterCase : SearchCharacterCase
: ViewModel

does anybody have any clue how to solve?
thank you very much

    dependencies {

implementation'androidx.room:room-runtime:2.5.0-alpha01'
annotationProcessor'androidx.room:room-compiler:2.5.0-alpha01'
implementation 'androidx.annotation:annotation:1.4.0-alpha02'
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
implementation 'androidx.fragment:fragment-ktx:1.4.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.0-alpha04'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.0-alpha04'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.0-alpha04'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation "com.google.dagger:hilt-android:2.31.2-alpha"
implementation 'com.google.dagger:dagger:2.31.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' 
      

//this is the ViewModel class

    import android.util.Log
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.viewModelScope
  import com.alons.marvel_universe.domain.use_cases.CharactersUseCase
import com.alons.marvel_universe.domain.use_cases.SearchCharacterCase
    import com.alons.marvel_universe.util.States
    import dagger.hilt.android.lifecycle.HiltViewModel
    import kotlinx.coroutines.Dispatchers
    import kotlinx.coroutines.flow.*
    import kotlinx.coroutines.launch
    import javax.inject.Inject


   @ HiltViewModel


    class CharactersViewModel @Inject constructor(

private val charactersUseCase : CharactersUseCase,
private val searchCharacterCase : SearchCharacterCase
) : ViewModel(){

private val marvelValue = MutableStateFlow(MarvelListState())
var _marvelValue : StateFlow<MarvelListState> = marvelValue

fun getSearchedCharacters(search:String)=viewModelScope.launch(Dispatchers.IO){
    searchCharacterCase.invoke(search=search).collect {
        when(it){
            is States.Success ->{
                marvelValue.value = MarvelListState(characterList = it.data?: emptyList())
                Log.d("toCharacter",_marvelValue.value.toString())
            }
            is States.Loading ->{
                marvelValue.value = MarvelListState(isLoading = true)
                Log.d("loading",it.data.toString())
            }
            is States.Error ->{
                marvelValue.value = MarvelListState(error = it.message?:"An Unexpected Error")
                Log.d("Error",it.data.toString())
            }
        }
    }
}
fun getAllCharactersData(offset:Int)=viewModelScope.launch(Dispatchers.IO){
    charactersUseCase(offset).collect {
        when(it){
            is States.Success ->{
                marvelValue.value = MarvelListState(characterList = it.data?: emptyList())
                Log.d("toCharacter",_marvelValue.value.toString())
            }
            is States.Loading ->{
                marvelValue.value = MarvelListState(isLoading = true)
                Log.d("loading",it.data.toString())
            }
            is States.Error ->{
                marvelValue.value = MarvelListState(error = it.message?:"An Unexpected Error")
                Log.d("Error",it.data.toString())
            }
        }
    }
}

}

2

Answers


  1. First, you should annotate @HiltViewModel for your viewmodel classes.
    Second, you should provide usecases as @Singleton or @ViewModelScoped for dagger-hilt to know what to inject into viewmodel.

    Login or Signup to reply.
  2. 1.You need to add @AndroidEntryPoint for your Activity(or fragment,depends on what you use).
    2.And of course @HiltViewModel for your ViewModel class

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search