我目前正在使用一个倒计时应用程序,每当我打开它时,它都会失败并显示一条消息Cannot create instance of class com.nailuj29gaming.CountdownViewModel
堆栈跟踪为
Process: com.nailuj29gaming.countdown.debug, PID: 17800
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nailuj29gaming.countdown.debug/com.nailuj29gaming.countdown.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.nailuj29gaming.countdown.CountdownViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2750)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2811)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.nailuj29gaming.countdown.CountdownViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at com.nailuj29gaming.countdown.MainActivity.onCreate(MainActivity.kt:23)
at android.app.Activity.performCreate(Activity.java:6757)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2703)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2811)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: java.lang.InstantiationException: java.lang.Class<com.nailuj29gaming.countdown.CountdownViewModel> 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.get(ViewModelProvider.java:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at com.nailuj29gaming.countdown.MainActivity.onCreate(MainActivity.kt:23)
at android.app.Activity.performCreate(Activity.java:6757)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2703)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2811)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
我知道有一个very similar question, this one,但这是针对Java的,我的代码是用Kotlin编写的。我的
ViewModel
类的文本是package com.nailuj29gaming.countdown
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
class CountdownViewModel(application: Application) : AndroidViewModel(application) {
private val repo: CountdownRepo
val countdowns: LiveData<List<Countdown>>
init {
val countdownDao = AppDatabase.getDatabase(application).countdownDao()
repo = CountdownRepo(countdownDao)
countdowns = repo.countdowns
}
fun insert(countdown: Countdown) = viewModelScope.launch {
repo.insert(countdown)
}
fun delete(countdown: Countdown) = viewModelScope.launch {
repo.delete(countdown)
}
}
我在
ViewModel
方法的第一行中创建MainActivity.onCreate()
。我用来创建它的代码是viewModel = ViewModelProvider(this).get(CountdownViewModel::class.java)
我的build.gradle文件(至少在此问题中很重要)是
apply plugin: 'kotlin-kapt'
dependencies {
def room_version = "2.2.3"
def lifecycle_version = "2.2.0"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'androidx.preference:preference:1.1.0'
implementation 'net.steamcrafted:materialiconlib:1.1.5'
implementation 'com.google.android.material:material:1.0.0'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.room:room-ktx:$room_version"
}
我尝试使用工厂,但不推荐使用ViewModelProviders类
当我使用委托(delegate)时,IDE并没有给我一个错误,但是编译器说
Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6. Please specify proper '-jvm-target' option
我的代码被编译为使用JVM target 1.8构建的字节码,因此不应显示此错误,对吗?
最佳答案
您需要升级到Fragment 1.2.0或更高版本,这就是根据Lifecycle 2.2.0 release notes添加对ViewModelProvider
构造函数的支持的原因:
使用较旧版本的Fragments(即AppCompat 1.1.0插入的Fragment 1.1.0)时,您只会获得在异常跟踪中看到的ViewModelProvider$NewInstanceFactory
,而不是支持AndroidViewModel
的ViewModel.Factory。
因此,您应该添加对片段1.2.1(当前最新版本)的显式依赖项:
implementation 'androidx.fragment:fragment:1.2.1'