无法创建MainViewModel的实例-没有Compose Hilt的零参数构造函数



使用Jetpack Compose构建小型Room数据库应用程序。我不断收到错误:

Cannot create an instance of class com.learning.kotlinreadexstingroomdb.MainViewModel
...
Caused by: java.lang.InstantiationException: java.lang.Class<com.learning.kotlinreadexstingroomdb.MainViewModel> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)

主要活动.kt

package com.learning.kotlinreadexstingroomdb
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import com.learning.kotlinreadexstingroomdb.ui.theme.KotlinReadExstingRoomDBTheme
class MainActivity : ComponentActivity() {
private val mainViewModel: MainViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
KotlinReadExstingRoomDBTheme {
MyApp()
}
}
}
@Composable
fun MyApp() {
val result by mainViewModel.readAll.collectAsState(initial= emptyList())
Column(
modifier = androidx.compose.ui.Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
if (result.isNotEmpty()) {
Text(
//text = person.name,
text = "There is Data!",
fontSize = MaterialTheme.typography.h4.fontSize
)
} else {
Text(
text = "Empty Database",
fontSize = MaterialTheme.typography.h4.fontSize
)
}
}
}
}

MainViewModel.kt

package com.learning.kotlinreadexstingroomdb
import androidx.lifecycle.ViewModel
import com.learning.kotlinreadexstingroomdb.data.PersonRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
@HiltViewModel
class MainViewModel
@Inject
constructor(personRepository: PersonRepository): ViewModel(){
val readAll = personRepository.readAll
}

build.gradle(:应用程序(

plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'                                            
}
...
dependencies {
def room_version = "2.4.1"                                              
implementation("androidx.room:room-runtime:$room_version")              
annotationProcessor("androidx.room:room-compiler:$room_version")        
def dagger_hilt_version = "2.40.5"
implementation "com.google.dagger:hilt-android:$dagger_hilt_version"    
kapt "com.google.dagger:hilt-compiler:$dagger_hilt_version"             
def compose_version = "1.0.5"                                           
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.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
implementation 'androidx.activity:activity-compose:1.4.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
}

我觉得我已经尝试了我在网上能找到的一切,但没有什么能解决它。

注:代码基于:已加载数据的预填充ROOM数据库|Android Studio教程

确保对应用程序类进行注释以初始化Hilt

@HiltAndroidApp
class App : Application()

如果您的应用程序类尚未在清单中注册,则需要在清单中进行注册。

<application
android:name=".App"
... />

您还需要对活动和片段进行注释,以便Hilt可以将其注入其中。

@AndroidEntryPoint
class MainActivity

在主活动中添加@AndroidEntryPoint注释

@AndroidEntryPoint // this line 
class MainActivity : ComponentActivity() {
private val mainViewModel: MainViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
KotlinReadExstingRoomDBTheme {
MyApp()
}
}
}

最新更新