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

jjjwad0x  于 2023-04-10  发布在  Android
关注(0)|答案(3)|浏览(223)

我已经检查了这些问题和答案,但仍然没有得到解决方案Question 1Question 2Question 3Question 4Question 5Question 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>()
}

这是活动课

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) 
        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) 
33qvvth1

33qvvth11#

请使用下面的代码初始化视图模型。
viewModel = ViewModelProviders.of(this).get(LoginViewModel::class.java)
如果它不工作,请尝试下面的代码
viewModel = ViewModelProvider(this,ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(LoginViewModel::class.java)

55ooxyrt

55ooxyrt2#

原因:java.lang.InstantiationException:java.lang.Class<com.vision.movies.viewModel.LoginViewModel>没有零参数构造函数
这意味着框架期望LoginViewModel(和任何其他视图模型)有一个0参数的构造函数。你没有,所以它崩溃了。在编译时找不到它很可能是由于使用了反射

ttp71kqs

ttp71kqs3#

如果您确实需要访问viewModel extend AndroidViewModel中的上下文,那么将context注入到viewModel中不是一个好主意
检查this question

相关问题