无法创建ViewModel类的实例或在Kotlin中没有零参数构造函数。

huangapple go评论58阅读模式
英文:

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 &lt;T : ViewModel&gt; create(modelClass: Class&lt;T&gt;): T {
if (modelClass.isAssignableFrom(LoginViewModel::class.java)) {
return LoginViewModel(context) as T
}
throw IllegalArgumentException(&quot;Unknown class name&quot;)
}
}

This is ViewModel class

class LoginViewModel (val context : Context) : ViewModel() {
var isTask = MutableLiveData&lt;Boolean&gt;()
}

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)&#160;
at android.app.ActivityThread.-wrap11(Unknown Source:0)&#160;
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630)&#160;
at android.os.Handler.dispatchMessage(Handler.java:106)&#160;
at android.os.Looper.loop(Looper.java:164)&#160;
at android.app.ActivityThread.main(ActivityThread.java:6626)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)&#160;
Caused by: java.lang.InstantiationException: java.lang.Class&lt;com.vision.movies.viewModel.LoginViewModel&gt; 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)&#160;
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:282)&#160;
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187)&#160;
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153)&#160;
at com.vision.movies.view.ActivityLogin.initViewModel(ActivityLogin.kt:29)&#160;
at com.vision.movies.view.ActivityLogin.onCreate(ActivityLogin.kt:23)&#160;
at android.app.Activity.performCreate(Activity.java:7032)&#160;
at android.app.Activity.performCreate(Activity.java:7023)&#160;
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1236)&#160;
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)&#160;
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943)&#160;
at android.app.ActivityThread.-wrap11(Unknown Source:0)&#160;
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630)&#160;
at android.os.Handler.dispatchMessage(Handler.java:106)&#160;
at android.os.Looper.loop(Looper.java:164)&#160;
at android.app.ActivityThread.main(ActivityThread.java:6626)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)&#160;

答案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

huangapple
  • 本文由 发表于 2023年4月4日 14:44:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定