我在运行应用程序时遇到异常。
FATAL EXCEPTION: main (Ask Gemini)
Process: com.nishant.comicslibrary, PID: 29957
ava.lang.IllegalArgumentException: CreationExtras must have a value by `SAVED_STATE_REGISTRY_OWNER_KEY`
at androidx.lifecycle.SavedStateHandleSupport.createSavedStateHandle(SavedStateHandleSupport.kt:89)
at
dagger.hilt.android.internal.lifecycle.HiltViewModelFactory$2.create(HiltViewModelFactory.java:100)
at
dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:170)
at androidx.lifecycle.ViewModelProvider$Factory.create(ViewModelProvider.android.kt:158)
at androidx.lifecycle.viewmodel.ViewModelProviderImpl.getViewModel$lifecycle_viewmodel_release(ViewModelProviderImpl.kt:69)
at androidx.lifecycle.viewmodel.ViewModelProviderImpl.getViewModel$lifecycle_viewmodel_release$default(ViewModelProviderImpl.kt:47)
这是我的主要活动
class MainActivity : ComponentActivity() {
//private val lvm by viewModels<LibraryApiViewModel>()
private lateinit var navController: NavHostController
private val lvm: LibraryApiViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
ComicsLibraryTheme {
navController = rememberNavController()
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
CharactersScaffold(navController = navController, lvm = lvm)
}
}
}
}
}
@Composable
fun CharactersScaffold(navController: NavHostController, lvm: LibraryApiViewModel) {
//val ScaffoldState = rememberScaffoldState()
val snackbarHostState = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()
Scaffold(
snackbarHost = { SnackbarHost(snackbarHostState) },
//scaffoldState = ScaffoldState,
bottomBar = { CharacterBottomNav(navController = navController)}
) { paddingValues ->
NavHost(
navController = navController,
startDestination = Destination.Library.route,
modifier = Modifier.padding(paddingValues)
) {
composable(Destination.Library.route) {
LibraryScreen(navController, lvm, paddingValues)
}
composable(Destination.Collection.route) {
CollectionScreen()
}
composable(Destination.CharacterDetail.route) {navBackStackEntery ->
}
}
}
}
ViewModel 类
@HiltViewModel
class LibraryApiViewModel @Inject constructor(
private val repo: MarvelApiRepo
): ViewModel() {
// View model code
}
我的 lib.version.toml 文件
[versions]
agp = "8.4.1"
kotlin = "1.9.0"
coreKtx = "1.13.1"
junit = "4.13.2"
junitVersion = "1.1.5"
espressoCore = "3.5.1"
lifecycleRuntimeKtx = "2.8.1"
activityCompose = "1.9.0"
composeBom = "2024.05.00"
hilt = "2.51.1"
navVersion = "2.7.7"
coilVersion = "2.1.0"
lifecycleVersion = "2.8.1"
retrofitVersion = "2.11.0"
roomVersion = "2.6.1"
[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
google-dagger-hilt = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt" }
google-dagger-hilt-compiler = { group = "com.google.dagger", name = "hilt-compiler", version.ref = "hilt" }
androidx-navigation = {group = "androidx.navigation", name = "navigation-compose", version.ref = "navVersion"}
io-coil = {group = "io.coil-kt", name = "coil-compose", version.ref = "coilVersion"}
androidx-viewmodel-compose = {group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "lifecycleVersion"}
com-squareup-retrofit = {group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofitVersion"}
com-squareup-retrofit-converter-gson = {group = "com.squareup.retrofit2", name = "converter-gson", version.ref = "retrofitVersion"}
androidx-room = {group = "androidx.room", name = "room-runtime", version.ref = "roomVersion"}
androidx-room-compiler = {group = "androidx.room", name = "room-compiler", version.ref = "roomVersion"}
androidx-room-ktx = {group = "androidx.room", name = "room-ktx", version.ref = "roomVersion"}
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
androidx-room = {id = "androidx.room", version.ref = "roomVersion"}
我的项目级别build.gradle文件
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.jetbrains.kotlin.android) apply false
alias(libs.plugins.androidx.room) apply false
alias(libs.plugins.kotlin.kapt) apply false
alias(libs.plugins.hilt) apply false
}
我的应用程序级别 build.gradle 文件
import java.util.Properties
import java.io.FileInputStream
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.jetbrains.kotlin.android)
alias(libs.plugins.androidx.room)
alias(libs.plugins.hilt)
alias(libs.plugins.kotlin.kapt)
}
val apikeyPropertiesFile = rootProject.file("apikey.properties")
val apikeyProperties = Properties()
apikeyProperties.load(FileInputStream(apikeyPropertiesFile))
android {
namespace = "com.nishant.comicslibrary"
compileSdk = 34
defaultConfig {
applicationId = "com.nishant.comicslibrary"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
buildConfigField("String", "MARVEL_KEY", apikeyProperties.getProperty("MARVEL_KEY"))
buildConfigField("String", "MARVEL_SECRET", apikeyProperties.getProperty("MARVEL_SECRET"))
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
room {
schemaDirectory("$projectDir/schemas")
}
}
dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
//hilt
implementation(libs.google.dagger.hilt)
kapt(libs.google.dagger.hilt.compiler)
//navigation
implementation(libs.androidx.navigation)
//coil
implementation(libs.io.coil)
//viewmodel compose
implementation(libs.androidx.viewmodel.compose)
//Retrofit
implementation(libs.com.squareup.retrofit)
implementation(libs.com.squareup.retrofit.converter.gson)
//room
implementation(libs.androidx.room)
kapt(libs.androidx.room.compiler)
implementation(libs.androidx.room.ktx)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
}
kapt {
correctErrorTypes = true
}
通过查看您的代码,我认为您错过的部分是,您没有使用
MainActivity
注释来注释您的 @AndroidEntryPoint
以便刀柄正常工作。因此,hilt 无法在 MainActivity 中注入 LibraryApiViewModel
的实例,从而引发错误。