androidkotlinviewmodeldatastore

java.lang.RuntimeException: Cannot create an instance of class ViewModel [Kotlin]


I created Viewmodel instance and it worked few times, now it is showing error while initializing userPermissionViewModel in MainActivity.

I am using DataStore to save data and try to observer the same.

I even tried initializing as below, but no luck

userPermissionsViewModel = ViewModelProvider(this,ViewModelProvider.AndroidViewModelFactory.getInstance(this.application)).get(UserPermissionsViewModel::class.java)

Error Log

2020-12-11 16:13:40.785 13161-13161/com.lobostaffing.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lobostaffing.app, PID: 13161
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lobostaffing.app/com.lobostaffing.app.mvvm.ui.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.lobostaffing.app.mvvm.viewmodels.UserPermissionsViewModel
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: java.lang.RuntimeException: Cannot create an instance of class com.lobostaffing.app.mvvm.viewmodels.UserPermissionsViewModel
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
    at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
    at com.lobostaffing.app.mvvm.ui.MainActivity.onCreate(MainActivity.kt:231)
    at android.app.Activity.performCreate(Activity.java:8000)
    at android.app.Activity.performCreate(Activity.java:7984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
 Caused by: java.lang.InstantiationException: java.lang.Class<com.lobostaffing.app.mvvm.viewmodels.UserPermissionsViewModel> has no zero argument constructor
    at java.lang.Class.newInstance(Native Method)
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
    at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278) 
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187) 
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) 
    at com.lobostaffing.app.mvvm.ui.MainActivity.onCreate(MainActivity.kt:231) 
    at android.app.Activity.performCreate(Activity.java:8000) 
    at android.app.Activity.performCreate(Activity.java:7984) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

MainActivity.kt

userPermissionsViewModel = ViewModelProvider(this).get(UserPermissionsViewModel::class.java)
    userPermissionsViewModel.readFromDataStore.observe(this, { userDetailModel ->
        setupNavDrawer(userDetailModel)
    })

UserPermissionsViewModel.kt

class UserPermissionsViewModel(application: Application): ViewModel() {

private val userPermissionsRepo = UserPermissionsRepo(application)

val readFromDataStore = userPermissionsRepo.userDetailModel.asLiveData()

fun saveToDataStore(userDetailModel: UserDetailModel) = viewModelScope.launch(Dispatchers.IO){
    userPermissionsRepo.saveUserPermissions(userDetailModel)
}}

Build.gradle

def archLifecycleVersion = '2.2.0'
//Lifecycle components
implementation "androidx.lifecycle:lifecycle-extensions:$archLifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$archLifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$archLifecycleVersion"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$archLifecycleVersion"

Solution

  • Try this

    MainActivity:

    class MainActivity : AppCompatActivity() {
    
        private lateinit var userPermissionsViewModel: UserPermissionsViewModel
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            userPermissionsViewModel = ViewModelProvider(this).get(UserPermissionsViewModel::class.java)
    
        }
    }
    

    And your viewModel:

    class UserPermissionsViewModel(application: Application) : AndroidViewModel(application) {
    
        private val userPermissionsRepo = UserPermissionsRepo(application)
    
    }