Я пытаюсь реализовать компоненты архитектуры в приложении, но столкнулся с проблемой при создании экземпляра 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)
Активность
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 -> {}
}
}
}
ViewModel
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")
}
}
BaseViewModel
open class BaseViewModel<N>(applicationClass: ApplicationClass) :AndroidViewModel(applicationClass) {
var mNavigator: N? = null
}