我创建了Viewmodel实例,它工作了几次,现在它在MainActivity中初始化userPermissionViewModel时显示错误。
我使用DataStore来保存数据,并尝试观察数据。
我甚至尝试初始化如下,但没有运气
userPermissionsViewModel = ViewModelProvider(this,ViewModelProvider.AndroidViewModelFactory.getInstance(this.application)).get(UserPermissionsViewModel::class.java)
错误日志
2020-12-11 16:13:40.785 13161-13161/com.lobostaffing.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lobostaffing.app, PID: 13161
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lobostaffing.app/com.lobostaffing.app.mvvm.ui.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.lobostaffing.app.mvvm.viewmodels.UserPermissionsViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.lobostaffing.app.mvvm.viewmodels.UserPermissionsViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at com.lobostaffing.app.mvvm.ui.MainActivity.onCreate(MainActivity.kt:231)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.InstantiationException: java.lang.Class<com.lobostaffing.app.mvvm.viewmodels.UserPermissionsViewModel> 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$AndroidViewModelFactory.create(ViewModelProvider.java:278)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at com.lobostaffing.app.mvvm.ui.MainActivity.onCreate(MainActivity.kt:231)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
主要活动.kt
userPermissionsViewModel = ViewModelProvider(this).get(UserPermissionsViewModel::class.java)
userPermissionsViewModel.readFromDataStore.observe(this, { userDetailModel ->
setupNavDrawer(userDetailModel)
})
用户权限ViewModel.kt
class UserPermissionsViewModel(application: Application): ViewModel() {
private val userPermissionsRepo = UserPermissionsRepo(application)
val readFromDataStore = userPermissionsRepo.userDetailModel.asLiveData()
fun saveToDataStore(userDetailModel: UserDetailModel) = viewModelScope.launch(Dispatchers.IO){
userPermissionsRepo.saveUserPermissions(userDetailModel)
}}
构建.gradle
def archLifecycleVersion = '2.2.0'
//Lifecycle components
implementation "androidx.lifecycle:lifecycle-extensions:$archLifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$archLifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$archLifecycleVersion"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$archLifecycleVersion"
试试这个
主要活动:
class MainActivity : AppCompatActivity() {
private lateinit var userPermissionsViewModel: UserPermissionsViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
userPermissionsViewModel = ViewModelProvider(this).get(UserPermissionsViewModel::class.java)
}
}
以及您的视图型号:
class UserPermissionsViewModel(application: Application) : AndroidViewModel(application) {
private val userPermissionsRepo = UserPermissionsRepo(application)
}
像这样初始化视图模型
yourViewModelInstance = new ViewModelProvider(this).get(YourViewModelClass.class);
当您在ViewModel中需要应用程序上下文时,它应该扩展AndroidViewModel
带上下文的ViewModel:
class UserPermissionsViewModel(application: Application) : AndroidViewModel(application)
或没有上下文
class UserPermissionsViewModel : ViewModel()
当常规ViewModel有任何构造函数参数时,默认的ViewModelFactory不知道如何创建它
尝试添加正确的依赖项
//lifecycle
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"