无法创建 Viewmodel 刀柄的实例

Cannot create instance of Viewmodel hilt

正在按照 mvvm 架构构建应用程序。但是当我尝试从视图模型调用方法时发生崩溃。在这里阅读类似的问题,但仍然遇到崩溃

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.track_it, PID: 17450
    java.lang.RuntimeException: Cannot create an instance of class com.fredosuala.track_it.data.viewmodel.TrackerViewModel
        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:112)
        at androidx.hilt.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:81)
        at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:69)
        at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:84)
        at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:108)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
        at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54)
        at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
        at com.fredosuala.track_it.ui.fragments.SetupFragment.getViewModel(Unknown Source:2)
        at com.fredosuala.track_it.ui.fragments.SetupFragment.trackThis(SetupFragment.kt:75)
        at com.fredosuala.track_it.ui.fragments.SetupFragment.access$trackThis(SetupFragment.kt:25)
        at com.fredosuala.track_it.ui.fragments.SetupFragment$onViewCreated.onClick(SetupFragment.kt:35)
        at android.view.View.performClick(View.java:7125)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View.performClickInternal(View.java:7102)
        at android.view.View.access00(View.java:801)
        at android.view.View$PerformClick.run(View.java:27336)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.InstantiationException: java.lang.Class<com.fredosuala.track_it.data.viewmodel.TrackerViewModel> has no zero argument constructor
        at java.lang.Class.newInstance(Native Method)
        at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278) 
        at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112) 
        at androidx.hilt.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:81) 

这是我的仓库

@Singleton
class Repository @Inject constructor(private val dao: TrackerDao) 

视图模型

@HiltViewModel
class TrackerViewModel @Inject constructor(private val repository: Repository) :ViewModel(){

这就是它在片段中的初始化方式

private val viewModel: TrackerViewModel by viewModels()

片段和 activity 都有 @AndroidEntryPoint 这是依赖项

 //Dagger - Hilt
    implementation "com.google.dagger:hilt-android:2.31-alpha"
    kapt "com.google.dagger:hilt-android-compiler:2.29.1-alpha"
    kapt 'androidx.hilt:hilt-compiler:1.0.0'
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"

类路径classpath "com.google.dagger:hilt-android-gradle-plugin:2.28.3-alpha"

问题是依赖项,我必须更新它们

implementation "com.google.dagger:hilt-android:2.37"
    kapt "com.google.dagger:hilt-android-compiler:2.37"
    kapt 'androidx.hilt:hilt-compiler:1.0.0'

和类路径

classpath "com.google.dagger:hilt-android-gradle-plugin:2.37"