I have a view model. I am using Hilt. I can create this view model from activity, like this
val model: ProfileViewModel by viewModels()
However when I try to create this view model from a composable function
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.viewmodel.compose.viewModel
@Composable
fun ProfileScreen(){
val model: ProfileViewModel = viewModel()
Button(onClick = {
model.logout()
}){
Text(stringResource(R.string.log_out))
}
}
I am getting error
2021-04-22 10:01:55.503 10482-10482/com.pulsariodev E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pulsariodev, PID: 10482
java.lang.RuntimeException: Cannot create an instance of class com.pulsario.ui.profile.ProfileViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at androidx.lifecycle.viewmodel.compose.ViewModelKt.get(ViewModel.kt:78)
at androidx.lifecycle.viewmodel.compose.ViewModelKt.viewModel(ViewModel.kt:63)
at com.pulsario.ui.profile.ProfileScreenKt.ProfileScreen(ProfileScreen.kt:23)
at com.pulsario.ui.main.MainScreenKt$MainScreen$2$1$1$1.invoke(MainScreen.kt:60)
at com.pulsario.ui.main.MainScreenKt$MainScreen$2$1$1$1.invoke(MainScreen.kt:59)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:118)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.navigation.compose.NavHostKt$NavHost$5$1$1.invoke(NavHost.kt:138)
at androidx.navigation.compose.NavHostKt$NavHost$5$1$1.invoke(NavHost.kt:137)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:193)
at androidx.compose.runtime.saveable.SaveableStateHolderImpl.SaveableStateProvider(SaveableStateHolder.kt:84)
at androidx.navigation.compose.NavHostKt.SaveableStateProvider(NavHost.kt:150)
at androidx.navigation.compose.NavHostKt.access$SaveableStateProvider(NavHost.kt:1)
at androidx.navigation.compose.NavHostKt$NavHost$5$1.invoke(NavHost.kt:137)
at androidx.navigation.compose.NavHostKt$NavHost$5$1.invoke(NavHost.kt:136)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:193)
at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:132)
at androidx.navigation.compose.NavHostKt$NavHost$6.invoke(Unknown Source:13)
at androidx.navigation.compose.NavHostKt$NavHost$6.invoke(Unknown Source:10)
at androidx.compose.runtime.RecomposeScopeImpl.compose(RecomposeScopeImpl.kt:97)
at androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd(Composer.kt:2117)
at androidx.compose.runtime.ComposerImpl.skipCurrentGroup(Composer.kt:2375)
at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:2517)
at androidx.compose.runtime.ComposerImpl.recompose$runtime_release(Composer.kt:2488)
at androidx.compose.runtime.CompositionImpl.recompose(Composition.kt:546)
at androidx.compose.runtime.Recomposer.performRecompose(Recomposer.kt:733)
at androidx.compose.runtime.Recomposer.access$performRecompose(Recomposer.kt:102)
at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:443)
at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:415)
at androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame(AndroidUiFrameClock.android.kt:34)
at androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109)
at androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41)
at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:69)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:965)
at android.view.Choreographer.doCallbacks(Choreographer.java:791)
2021-04-22 10:01:55.504 10482-10482/com.pulsariodev E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:722)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:952)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7386)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
Caused by: java.lang.InstantiationException: java.lang.Class<com.pulsario.ui.profile.ProfileViewModel> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
... 52 more
Here is the example from Android tutorial but this does not work for me.