英文:
Cannot create an instance of class ViewModel or has no zero argument constructor in Kotlin
问题
我已经检查了这些问题和答案,但仍然没有找到解决方案问题 1,问题 2,问题 3,问题 4,问题 5,问题 6,并且还检查了一些文章。
这是我的Factory
类
class ContextViewModelFactory (private val context: Context) :
ViewModelProvider.AndroidViewModelFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(LoginViewModel::class.java)) {
return LoginViewModel(context) as T
}
throw IllegalArgumentException("Unknown class name")
}
}
这是ViewModel
类
class LoginViewModel (val context : Context) : ViewModel() {
var isTask = MutableLiveData<Boolean>()
}
这是Activity类
class ActivityLogin : BaseActivity() {
lateinit var binding: ActivityLoginBinding
lateinit var viewModel: LoginViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding.root)
observer()
}
private fun initViewModel() {
viewModel = ViewModelProvider(this, ContextViewModelFactory(this@ActivityLogin))[LoginViewModel::class.java]
binding.model = viewModel
binding.lifecycleOwner = this
}
}
我已经尝试了很多次,但还没有解决,能否帮助我找出我做错了什么,谢谢。
以下是错误日志
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vision.movies/com.vision.movies.view.ActivityLogin}: java.lang.RuntimeException: Cannot create an instance of class com.vision.movies.viewModel.LoginViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2861)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6626)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.vision.movies.viewModel.LoginViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:204)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:322)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:282)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153)
at com.vision.movies.view.ActivityLogin.initViewModel(ActivityLogin.kt:29)
at com.vision.movies.view.ActivityLogin.onCreate(ActivityLogin.kt:23)
at android.app.Activity.performCreate(Activity.java:7032)
at android.app.Activity.performCreate(Activity.java:7023)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1236)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6626)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: java.lang.InstantiationException: java.lang.Class<com.vision.movies.viewModel.LoginViewModel> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:202)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:322)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:282)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153)
at com.vision.movies.view.ActivityLogin.initViewModel(ActivityLogin.kt:29)
at com.vision.movies.view.ActivityLogin.onCreate(ActivityLogin.kt:23)
at android.app.Activity.performCreate(Activity.java:7032)
at android.app.Activity.performCreate(Activity.java:7023)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1236)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943)
英文:
I have checked these are the question and answer but still not getting solution Question 1, Question 2, Question 3, Question 4, Question 5, Question 6 and also checked some of the article.
This is my Factory
class
class ContextViewModelFactory (private val context: Context) :
ViewModelProvider.AndroidViewModelFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(LoginViewModel::class.java)) {
return LoginViewModel(context) as T
}
throw IllegalArgumentException("Unknown class name")
}
}
This is ViewModel
class
class LoginViewModel (val context : Context) : ViewModel() {
var isTask = MutableLiveData<Boolean>()
}
This is Activity class
class ActivityLogin : BaseActivity() {
lateinit var binding: ActivityLoginBinding
lateinit var viewModel: LoginViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding.root)
observer()
}
private fun initViewModel() {
viewModel = ViewModelProvider(this,
ContextViewModelFactory(this@ActivityLogin))[LoginViewModel::class.java]
binding.model = viewModel
binding.lifecycleOwner = this
}
}
I have tried so much but not solver, do some help that where I am doing something wrong, Thanks.
Below is the error log
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vision.movies/com.vision.movies.view.ActivityLogin}: java.lang.RuntimeException: Cannot create an instance of class com.vision.movies.viewModel.LoginViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2861)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6626)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.vision.movies.viewModel.LoginViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:204)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:322)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:282)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153)
at com.vision.movies.view.ActivityLogin.initViewModel(ActivityLogin.kt:29)
at com.vision.movies.view.ActivityLogin.onCreate(ActivityLogin.kt:23)
at android.app.Activity.performCreate(Activity.java:7032)
at android.app.Activity.performCreate(Activity.java:7023)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1236)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6626) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) 
Caused by: java.lang.InstantiationException: java.lang.Class<com.vision.movies.viewModel.LoginViewModel> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:202)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:322) 
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:282) 
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187) 
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153) 
at com.vision.movies.view.ActivityLogin.initViewModel(ActivityLogin.kt:29) 
at com.vision.movies.view.ActivityLogin.onCreate(ActivityLogin.kt:23) 
at android.app.Activity.performCreate(Activity.java:7032) 
at android.app.Activity.performCreate(Activity.java:7023) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1236) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6626) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) 
答案1
得分: 1
请使用以下代码来初始化 ViewModel。
viewModel = ViewModelProviders.of(this).get(LoginViewModel::class.java)
如果不起作用,请尝试以下代码。
viewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(LoginViewModel::class.java)
英文:
Please use below code to initialize viewmodel.
viewModel = ViewModelProviders.of(this).get(LoginViewModel::class.java)
If it not works try below code
viewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(LoginViewModel::class.java)
答案2
得分: 1
由于:java.lang.InstantiationException: java.lang.Class<com.vision.movies.viewModel.LoginViewModel> 没有零参数构造函数
这意味着框架期望LoginViewModel(以及任何其他视图模型)具有零参数构造函数。 但你没有,所以它会崩溃。很可能是由于使用反射,它在编译时无法找到。
英文:
Caused by: java.lang.InstantiationException: java.lang.Class<com.vision.movies.viewModel.LoginViewModel> has no zero argument constructor
This means that the framework expects LoginViewModel (and any other view model) to have a 0 argument constructor. You don't, so it crashes. It isn't found at compile time most likely due to a use of reflection
答案3
得分: 0
将context
注入viewModel
并不是一个好主意,如果你真的需要在viewModel
中访问context
,可以扩展AndroidViewModel
。
查看这个问题。
英文:
injecting the context
into the viewModel
is not a good idea, if you really need to access the context in the viewModel
extend AndroidViewModel
check this question
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论