17

Hello, I'm trying to inject view model using Hilt, but I get the following error:

 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.wordssample, PID: 25250
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wordssample/com.example.wordssample.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.wordssample.MainViewModel
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
  
 Caused by: java.lang.RuntimeException: Cannot create an instance of class com.example.wordssample.MainViewModel
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
    at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
    at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:106)
    at androidx.hilt.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:74)
    at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:69)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

This is my MainActivity:

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
   
    private val viewModel  by viewModels<MainViewModel>()


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

And this is the ViewModel class I'm trying to inject

class MainViewModel @ViewModelInject constructor(
    @ApplicationContext application: Context,
    @Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel() {

    private val repositorio = WordRepositorio(application)

    val allWords = repositorio.mAllWords
...

I appreciate the help! Thanks

Tarsila Costalonga
  • 924
  • 2
  • 11
  • 21
  • the code is fine, error somewhere else, also try to update activity / lifecycle libraries (at least to the latest stable release), https://developer.android.com/jetpack/androidx/releases/activity, https://developer.android.com/jetpack/androidx/releases/lifecycle. It's better to create a module that provides WordRepositorio and inject WordRepositorio instead of Context – IR42 Jul 29 '20 at 00:52
  • Did you add annotation `@HiltAndroidApp` to your Application class? – Liem Vo Jul 29 '20 at 02:51
  • Why don't you inject the application context to your repository and then inject your repository to your ViewModel? – hosseinAmini Apr 06 '21 at 06:18

2 Answers2

7

BaseViewModel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
open class BaseViewModel @Inject constructor(application: Application) : AndroidViewModel(application) {
  protected val context
    get() = getApplication<Application>()
}

HomeViewModel

@HiltViewModel
class HomeViewModel @Inject constructor(
  application: Application,
  private val userRepository: UserRepository
) : BaseViewModel(application) {
  val text1 = MutableLiveData(context.getString(R.string.string_1))

  fun update(){
    text1.value = context.getString(R.string.string_2)
  }
}

HomeFragment

@AndroidEntryPoint
class HomeFragment : Fragment(R.layout.home_fragment) {
  private val binding: HomeFragmentBinding by dataBinding()
  private val viewModel: HomeViewModel by viewModels()

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    binding.vm = viewModel
  }
Hun
  • 3,652
  • 35
  • 72
3

Yes, there was nothing wrong with the code. The problem was in some libraries, apparently I was missing something.

I solved it by adding:

implementation 'com.google.dagger:hilt-android:2.28-alpha'

implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02'

kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha02'

kapt 'com.google.dagger:hilt-android-compiler:2.28-alpha'

implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'

Tarsila Costalonga
  • 924
  • 2
  • 11
  • 21