目前我的活动类看起来是这样的:
@AndroidEntryPoint
class FamiliarActivity : AppCompatActivity() {
private val noticeScreenViewModel : NoticeScreenViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
FamiliarTheme {
val navController = rememberNavController()
val currentBackStack by navController.currentBackStackEntryAsState()
val currentDestination = currentBackStack?.destination
val currentScreen =
familiarScreens.find { it.route == currentDestination?.route } ?: Settings
FamiliarNavHost(
noticesViewModel = noticeScreenViewModel,
navController = navController,
modifier = Modifier.padding()
)
}
}
}
}
字符串
NoticeScreenViewModel看起来像这样:
@HiltViewModel
class NoticeScreenViewModel @Inject internal constructor(
private val noticeRepository: NoticeRepository
) : ViewModel(){
val notices: List<Notice> = noticeRepository.getNotices()
}
型
NoticeRepository看起来像这样:
class NoticeRepository @Inject constructor(
private val noticeDao: NoticeDao
) {
fun createNotice(notice: Notice) {
noticeDao.insert(notice)
}
fun removeNotice(notice: Notice) {
noticeDao.delete(notice)
}
fun getNotices() = noticeDao.getNotices()
companion object {
// For Singleton instantiation
@Volatile private var instance: NoticeRepository? = null
fun getInstance(noticeDao: NoticeDao) =
instance ?: synchronized(this) {
instance ?: NoticeRepository(noticeDao).also { instance = it }
}
}
}
型
NoticeDao看起来像这样:
@Dao
interface NoticeDao {
@Query("SELECT * FROM notice")
fun getNotices(): List<Notice>
@Insert
fun insertAll(vararg notices: Notice)
@Insert
fun insert(notice: Notice)
@Delete
fun delete(notice: Notice)
}
型
而且它从来都不起作用。每次,应用程序在初始化ViewModel时都会崩溃。是的,我已经尝试了我在类似帖子中找到的所有方法。我错过了什么?
我已经多次重新创建了所有这些类,但从未完成创建viewModel的步骤。
堆栈跟踪:
FATAL EXCEPTION: main Process: com.dmlpcb.familiar, PID: 27627 java.lang.RuntimeException: Cannot create an instance of class com.dmlpcb.familiar.ui.NoticeScreenViewModel at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:204) at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:324) at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:306) at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:280) at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.kt:128) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153) at androidx.lifecycle.ViewModelLazy.getValue(ViewModelLazy.kt:53) at androidx.lifecycle.ViewModelLazy.getValue(ViewModelLazy.kt:35) at com.dmlpcb.familiar.FamiliarActivity$noticeScreenViewModel$2.invoke(FamiliarActivity.kt:23) at com.dmlpcb.familiar.FamiliarActivity$noticeScreenViewModel$2.invoke(FamiliarActivity.kt:22) at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) at com.dmlpcb.familiar.FamiliarActivity.getNoticeScreenViewModel(FamiliarActivity.kt:22) at com.dmlpcb.familiar.FamiliarActivity.access$getNoticeScreenViewModel(FamiliarActivity.kt:19) at com.dmlpcb.familiar.FamiliarActivity$onCreate$1$1.invoke(FamiliarActivity.kt:36) at com.dmlpcb.familiar.FamiliarActivity$onCreate$1$1.invoke(FamiliarActivity.kt:28) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228) at androidx.compose.material3.TextKt.ProvideTextStyle(Text.kt:360) at androidx.compose.material3.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:81) at androidx.compose.material3.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:80) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228) at androidx.compose.material3.MaterialThemeKt.MaterialTheme(MaterialTheme.kt:73) at com.dmlpcb.familiar.compose.ThemesKt.FamiliarTheme(Themes.kt:22) at com.dmlpcb.familiar.FamiliarActivity$onCreate$1.invoke(FamiliarActivity.kt:28) at com.dmlpcb.familiar.FamiliarActivity$onCreate$1.invoke(FamiliarActivity.kt:27) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) at androidx.compose.ui.platform.ComposeView.Content(ComposeView.android.kt:428) at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:252) at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:251) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228) at androidx.compose.ui.platform.CompositionLocalsKt.ProvideCommonCompositionLocals(CompositionLocals.kt:195) at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:119) at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:118) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108) 2023-12-29 00:35:46.765 27627-27627 AndroidRuntime com.dmlpcb.familiar E at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228) at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.ProvideAndroidCompositionLocals(AndroidCompositionLocals.android.kt:110) at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$2.invoke(Wrapper.android.kt:158) at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$2.invoke(Wrapper.android.kt:157) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228) at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:157) at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:142) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) at androidx.compose.runtime.ActualJvm_jvmKt.invokeComposable(ActualJvm.jvm.kt:78) at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3340) at androidx.compose.runtime.ComposerImpl.composeContent$runtime_release(Composer.kt:3273) at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:588) at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:1013) at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:520) at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:142) at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:133) at androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(AndroidComposeView.android.kt:1191) at androidx.compose.ui.platform.WrappedComposition.setContent(Wrapper.android.kt:133) at androidx.compose.ui.platform.WrappedComposition.onStateChanged(Wrapper.android.kt:183) at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.kt:314) at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.kt:192) at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:140) at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:133) at androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.android.kt:1266) at android.view.View.dispatchAttachedToWindow(View.java:21980) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3490) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3014) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2465) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9305) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1339) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1348) at android.view.Choreographer.doCallbacks(Choreographer.java:952) at android.view.Choreographer.doFrame(Choreographer.java:882) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1322) at android.os.Handler.handleCallback(Handler.java:958) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:205) at android.os.Looper.loop(Looper.java:294) 2023-12-29 00:35:46.776 27627-27627 AndroidRuntime com.dmlpcb.familiar E at android.app.ActivityThread.main(ActivityThread.java:8177) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971) Caused by: java.lang.NoSuchMethodException: com.dmlpcb.familiar.ui.NoticeScreenViewModel. [] at java.lang.Class.getConstructor0(Class.java:3325) at java.lang.Class.getDeclaredConstructor(Class.java:3063) at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:202) ... 92 more
1条答案
按热度按时间dxxyhpgq1#
不要像这样声明viewModel:
private val noticeScreenViewModel : NoticeScreenViewModel by viewModels()
,而是在Composable中像这样声明:val noticeScreenViewModel : NoticeScreenViewModel = hiltViewModel()
。就像这样:
字符串
这里有一个例子。
另外,您没有显示您的AppModule和Hilt Application文件,可能是丢失了?