提问者:小点点

静态编程语言无法创建ViewModel的实例


我对我的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个位置。第一个是在我的MainActivity. kt类'onCreate()方法中启动viewModel对象:

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

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

        ...
    }

2ndpoint是我的存储库类中的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个答案

匿名用户

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

EntryViewModel中,您有这一行:

init {allEntries = repository.allEntries }

您正在引用EntryRepositoryallEntry属性。定义为lateinit

lateinit var allEntries: LiveData<List<Entry>>

该错误告诉您在尝试引用allEntry之前尚未为其赋值。如果您要在可见(非私有)属性上使用lateinit,则需要确保在任何其他对象尝试使用该属性之前为其赋值。