gpt4 book ai didi

android - 无法创建 AndroidViewModel 的实例

转载 作者:行者123 更新时间:2023-12-02 13:01:53 25 4
gpt4 key购买 nike

我正在尝试在应用程序中实现架构组件,但在创建 androidViewModel 实例时遇到了问题。请帮我解决这个问题。

我在这里尝试创建 LoginViewModel 类的实例,但出现以下错误

错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.addonitservices.uphaar, PID: 22975
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.addonitservices.uphaar/com.addonitservices.uphaar.login.LoginActivity}: java.lang.RuntimeException: Cannot create an instance of class com.addonitservices.uphaar.login.LoginViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
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:807)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.addonitservices.uphaar.login.LoginViewModel
at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:201)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102)
at com.addonitservices.uphaar.login.LoginActivity.initViewModel(LoginActivity.kt:27)
at com.addonitservices.uphaar.login.LoginActivity.onCreate(LoginActivity.kt:22)
at android.app.Activity.performCreate(Activity.java:6999)
at android.app.Activity.performCreate(Activity.java:6990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
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:807) 
Caused by: java.lang.NoSuchMethodException: <init> [class android.app.Application]
at java.lang.Class.getConstructor0(Class.java:2320)
at java.lang.Class.getConstructor(Class.java:1725)
at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:199)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134) 
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102) 
at com.addonitservices.uphaar.login.LoginActivity.initViewModel(LoginActivity.kt:27) 
at com.addonitservices.uphaar.login.LoginActivity.onCreate(LoginActivity.kt:22) 
at android.app.Activity.performCreate(Activity.java:6999) 
at android.app.Activity.performCreate(Activity.java:6990) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
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:807) 

Activity

import android.arch.lifecycle.ViewModelProvider
import android.arch.lifecycle.ViewModelProviders
import android.databinding.DataBindingUtil
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.addonitservices.uphaar.R
import com.addonitservices.uphaar.base.BaseActivity
import com.addonitservices.uphaar.databinding.ActivityLoginBinding
import com.mukesh.countrypicker.CountryPicker

class LoginActivity : BaseActivity() {

private lateinit var binding: ActivityLoginBinding
private lateinit var viewModel:LoginViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_login)
binding.view = this
initViewModel()

}

private fun initViewModel(){
viewModel = ViewModelProviders.of(this).get(LoginViewModel::class.java)
viewModel.initCountryPicker(getString(R.string.select_country))
binding.viewModel =viewModel
}


fun onClick(view: View) {
when (view.id) {

R.id.btnLogin -> {}

R.id.tvForgotPassword -> {}

R.id.layoutCountryCode -> viewModel.showCountryPickerDialog(supportFragmentManager)

R.id.tvSignUp -> {}
}
}



}

View 模型

import android.arch.lifecycle.MutableLiveData
import android.support.v4.app.FragmentManager
import com.addonitservices.uphaar.base.ApplicationClass
import com.addonitservices.uphaar.base.BaseViewModel
import com.mukesh.countrypicker.CountryPicker

/**
* Created by bms on 6/6/18 added in com.addonitservices.uphaar.login
*/
class LoginViewModel(applicationClass: ApplicationClass) : BaseViewModel<LoginNavigator>(applicationClass) {


val countryCode = MutableLiveData<String>()
val flagImage = MutableLiveData<Int>()
val mobileNumber = MutableLiveData<String>()
val password = MutableLiveData<String>()
private var countryPicker:CountryPicker? = null




fun initCountryPicker(message:String){
countryPicker = CountryPicker.newInstance(message)
countryPicker?.setListener { name, code, dialCode, flag ->
flagImage.value=flag
countryCode.value = dialCode
}
}

fun showCountryPickerDialog(fragmentManager:FragmentManager){
countryPicker?.show(fragmentManager,"COUNTRY_PICKER")
}
}

基础 View 模型

open class BaseViewModel<N>(applicationClass: ApplicationClass) :AndroidViewModel(applicationClass) {


var mNavigator: N? = null

}

最佳答案

LoginViewModel(applicationClass: ApplicationClass)

应该是

LoginViewModel(application: android.app.Application)

关于android - 无法创建 AndroidViewModel 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50755216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com