简体   繁体   English

java.lang.RuntimeException:当android重新创建Activity和Fragment时,无法创建类ViewModel的实例

[英]java.lang.RuntimeException: Cannot create an instance of class ViewModel, when android recreates Activity and Fragment

Case is of Activity-Fragment Recreate : I am creating ViewModel from activity context at activity level and trying to access the initialised ViewModel at Fragment level, using ViewModelProvider .of().案例是Activity-Fragment Recreate:我正在activity级别从活动context创建ViewModel ,并尝试使用ViewModelProvider .of() 在Fragment级别访问初始化的ViewModel

Below code works in happy case;下面的代码在快乐的情况下工作; but when activity is recreated (changes system language or permission), same code don't work, and fragment is not able to access the activity created viewModel.但是当重新创建活动(更改系统语言或权限)时,相同的代码不起作用,并且片段无法访问活动创建的视图模型。

ViewModel is parameterised and has only one primary constructor and initialised using ViewModelProvider.Factory ViewModel 是参数化的,只有一个主要构造函数,并使用 ViewModelProvider.Factory 进行初始化

ViewModel Structure视图模型结构

class MessageViewModel(var message: MutableLiveData<String>): ViewModel() { ... }

ViewModel Initialisation at Activity Activity 处的 ViewModel 初始化

val viewModel = ViewModelProviders.of(this, MessageVMFactory())
            .get(MessageViewModel::class.java)

Accessing ViewModel at Fragment在 Fragment 处访问 ViewModel

val viewModel = ViewModelProviders.of(activity)
            .get(MessageViewModel::class.java)

Error错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.krp.android.viewmodeldemo/com.krp.android.viewmodeldemo.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.krp.android.viewmodeldemo.utility.MessageViewModel java.lang.RuntimeException:无法启动活动 ComponentInfo{com.krp.android.viewmodeldemo/com.krp.android.viewmodeldemo.MainActivity}:java.lang.RuntimeException:无法创建类 com.krp.android.viewmodeldemo 的实例.utility.MessageViewModel

If you have initialised the ViewModel via Factory, you must access it via Factory, regardless of any context.如果您已通过 Factory 初始化 ViewModel,则必须通过 Factory 访问它,而不管任何上下文。 Seems ViewModel issue when system recreates activity or fragment.当系统重新创建活动或片段时,似乎是 ViewModel 问题。

It works fine at Fragment when它在 Fragment 上工作正常,当

val viewModel = ViewModelProviders.of(activity, MessageVMFactory())
                .get(MessageViewModel::class.java)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.RuntimeException:无法创建 ViewModel class 的实例 - java.lang.RuntimeException: Cannot create an instance of ViewModel class java.lang.RuntimeException:无法创建 class ViewModel [Kotlin] 的实例 - java.lang.RuntimeException: Cannot create an instance of class ViewModel [Kotlin] java.lang.RuntimeException:无法创建 class com.example.cookpadapp.viewmodel.CookpadViewModel+ 的实例 - java.lang.RuntimeException: Cannot create an instance of class com.example.cookpadapp.viewmodel.CookpadViewModel+ at java.lang.RuntimeException:无法创建 yodgorbek.komilov.musobaqayangiliklari.viewmodel.MainViewModel 类的实例? - java.lang.RuntimeException: Cannot create an instance of class yodgorbek.komilov.musobaqayangiliklari.viewmodel.MainViewModel? Jetpack Compose + Hilt:java.lang.RuntimeException:无法创建 class ViewModel 的实例 - Jetpack Compose + Hilt: java.lang.RuntimeException: Cannot create an instance of class ViewModel java.lang.RuntimeException:无法在单元测试中创建 class ViewModel 的实例 - java.lang.RuntimeException: Cannot create an instance of class ViewModel in unit test 刀柄匕首错误 java.lang.RuntimeException:无法创建 class HomeFragmentViewModel 的实例 - Hilt Dagger Error java.lang.RuntimeException: Cannot create an instance of class HomeFragmentViewModel java.lang.RuntimeException:无法创建类MovieViewModel的实例&存储库尚未初始化 - java.lang.RuntimeException: Cannot create an instance of class MovieViewModel& repository has not been initialized java.lang.RuntimeException:无法创建类 com.example.homeactivity.activities.editprofile.EditProfileViewModel 的实例 - java.lang.RuntimeException: Cannot create an instance of class com.example.homeactivity.activities.editprofile.EditProfileViewModel android-java.lang.RuntimeException-地图活动 - android - java.lang.RuntimeException - maps activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM