Kotlin无法创建ViewModel的实例

时间:2019-06-05 10:25:18

标签: android mvvm kotlin runtimeexception androidx

我的ViewModel出现以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cryptpass_kt/com.example.cryptpass_kt.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.cryptpass_kt.EntryViewModel
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2853)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2928)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1609)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6703)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
     Caused by: java.lang.RuntimeException: Cannot create an instance of class com.example.cryptpass_kt.EntryViewModel
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:238)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130)
        at com.example.cryptpass_kt.MainActivity.onCreate(MainActivity.kt:40)
        at android.app.Activity.performCreate(Activity.java:6980)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2806)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2928) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1609) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6703) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:230)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130) 
        at com.example.cryptpass_kt.MainActivity.onCreate(MainActivity.kt:40) 
        at android.app.Activity.performCreate(Activity.java:6980) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2806) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2928) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1609) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6703) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769) 
     Caused by: kotlin.UninitializedPropertyAccessException: lateinit property allEntries has not been initialized
        at com.example.cryptpass_kt.EntryRepository.getAllEntries(EntryRepository.kt:9)
        at com.example.cryptpass_kt.EntryViewModel.<init>(EntryViewModel.kt:11)
        at java.lang.reflect.Constructor.newInstance0(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334) 
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:230) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130) 
        at com.example.cryptpass_kt.MainActivity.onCreate(MainActivity.kt:40) 
        at android.app.Activity.performCreate(Activity.java:6980) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2806) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2928) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1609) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6703) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769) 

错误指向我应用中的3个位置。第一个是我的viewModel类的MainActivity.kt方法中onCreate()对象的初始化:

override fun onCreate(savedInstanceState: Bundle?) {
        ...

        viewModel = ViewModelProviders.of(this).get(EntryViewModel::class.java)
        viewModel.allEntries.observe(this, Observer { entries -> adapter.submitList(entries) })

        ...
    }

第二个点是我的存储库类中的LiveData对象:

class EntryRepository(application: Application) {
    private val entryDAO: EntryDAO
    lateinit var allEntries: LiveData<List<Entry>>

    ...
    }

第3点是ViewModel内的存储库对象:

class EntryViewModel(application: Application) : AndroidViewModel(application) {
    private val repository: EntryRepository = EntryRepository(application)
    val allEntries: LiveData<List<Entry>>

    init {allEntries = repository.allEntries }

    fun insert(entry: Entry) {repository.insert(entry)}

    fun update(entry: Entry) {repository.update(entry)}

    fun delete(entry: Entry) {repository.update(entry)}

    fun deleteAll() {repository.deleteAllEntries()}
}

我第一次遇到此错误时,在S / O上被告知,这是由于我的gradle文件中有一些与AnroidX不兼容的实现。解决此问题后,问题仍然存在。

1 个答案:

答案 0 :(得分:2)

Caused by: kotlin.UninitializedPropertyAccessException: lateinit property allEntries has not been initialized

EntryViewModel中,您有以下一行:

init {allEntries = repository.allEntries }

您引用的是allEntries的{​​{1}}属性。定义为EntryRepository

lateinit

该错误告诉您在尝试引用lateinit var allEntries: LiveData<List<Entry>> 之前尚未分配值。如果要在可见的(非allEntries)属性上使用lateinit,则需要确保在为其分配值之前,其他任何对象都可以尝试使用该属性。