我是 Android 开发新手,目前正在构建
我的第一个真正的应用程序。我正在尝试实现 MVVM 架构
正因为如此,我为每个 fragment 都有一个 viewModel 和
每个 viewModel 都有一个 viewModelFactory。至少,我是这样的
明白它必须是。
我使用每个人似乎都用于工厂的样板代码:
class ExampleViewModelFactory(private val exampleDao: ExampleDao) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(exampleViewModel::class.java)) {
@Suppress("UNCHECKED_CAST")
return ExampleViewModel(exampleDao) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
现在的问题是,编译器给了我以下错误:e: C:\Users\ ...\ExampleViewModel.kt: (64, 7): Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option
这个错误是由我在viewModel中实现的viewModelFactory类产生的。我真的不知道这意味着什么,我找不到任何有用的东西,甚至找不到与我的具体问题相关的东西。我基本上遵循了一些关于创建你的第一个应用程序的基本教程,但我不断遇到这样的错误。在大多数情况下,我能够自己解决问题,但这次不同。
我知道你们中的很多人都有很多经验和知识,所以我希望你们中的一些人能抽出时间来帮助我,给我一个提示,我可以做些什么来解决这个问题。
最佳答案
取决于 Lifecycle 2.5.0-alpha01
,您似乎是直接或间接(通过其他库) .
根据 this issue :
You need to temporarily add following to your
build.gradle
:tasks.withType(KotlinCompile).configureEach { kotlinOptions { freeCompilerArgs += [ "-Xjvm-default=all", ] } }
Note that in certain circumstances you may want to use
all-compatibility
instead ofall
, learn more about that in Jebrain's blogpost.Starting with kotlin 1.6.20 you won't need touch build.gradle for more information see KT-47000
关于android - 如何解决错误消息 : "Inheritance from an interface with ' @JvmDefault' members is only allowed with -Xjvm-default option",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70992947/