You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to access my ViewModel class from a Fragment as shown below:
//Error
private val viewModel: MyViewModel by viewModels()
//No Error
private val viewModel: MyViewModel by activityViewModels()
If no class is injected into my ViewModel class via its constructor, I can access it using this method without encountering any errors. However, when a class is injected into the ViewModel, I am unable to access it, and I encounter the following error:
Error;
2025-01-08 16:37:26.402 12233-12233 AndroidRuntime com.example.hilttutorial E FATAL EXCEPTION: main
Process: com.example.hilttutorial, PID: 12233
java.lang.RuntimeException: Cannot create an instance of class com.example.hilttutorial.presentation.my_fragment.MyViewModel
at androidx.lifecycle.viewmodel.internal.JvmViewModelProviders.createViewModel(JvmViewModelProviders.kt:40)
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.android.kt:193)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.android.kt:317)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.android.kt:299)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.android.kt:273)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.kt:128)
at androidx.lifecycle.ViewModelProvider$Factory.create(ViewModelProvider.android.kt:158)
at androidx.lifecycle.viewmodel.ViewModelProviderImpl_androidKt.createViewModel(ViewModelProviderImpl.android.kt:34)
at androidx.lifecycle.viewmodel.ViewModelProviderImpl.getViewModel$lifecycle_viewmodel_release(ViewModelProviderImpl.kt:65)
at androidx.lifecycle.viewmodel.ViewModelProviderImpl.getViewModel$lifecycle_viewmodel_release$default(ViewModelProviderImpl.kt:47)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.android.kt:91)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelLazy.kt:51)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelLazy.kt:35)
at com.example.hilttutorial.presentation.my_fragment.MyFragment.getViewModel(MyFragment.kt:15)
at com.example.hilttutorial.presentation.my_fragment.MyFragment.onViewCreated(MyFragment.kt:34)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3152)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:608)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:286)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:114)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1685)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3319)
at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:3230)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3153)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:608)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:286)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:114)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1685)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3319)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3237)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:263)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:350) at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:251)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1582)
at android.app.Activity.performStart(Activity.java:8628)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3807)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:225)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:205)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:177)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:98)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
2025-01-08 16:37:26.403 12233-12233 AndroidRuntime com.example.hilttutorial E at android.app.ActivityThread.main(ActivityThread.java:8177)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
Caused by: java.lang.NoSuchMethodException: com.example.hilttutorial.presentation.my_fragment.MyViewModel.<init> []
at java.lang.Class.getConstructor0(Class.java:3325)
at java.lang.Class.getDeclaredConstructor(Class.java:3063)
at androidx.lifecycle.viewmodel.internal.JvmViewModelProviders.createViewModel(JvmViewModelProviders.kt:38)
... 46 more
Note 1
On the other hand, when I try to access the ViewModel using the following method, I don't encounter any issues:
private val viewModel: MyViewModel by activityViewModels()
Note 2
To help better understand the issue, I am attaching the relevant ViewModel, Fragment, and Hilt module files to this report.
Yes, when I add the @androidentrypoint annotation to my 'MyFragment' class, I do not encounter any errors.
However, is this the correct approach? I have already added the @androidentrypoint annotation to my MainActivity class, and this implies that I need to add @androidentrypoint to each of my fragment classes as well.
I want to access my ViewModel class from a Fragment as shown below:
If no class is injected into my ViewModel class via its constructor, I can access it using this method without encountering any errors. However, when a class is injected into the ViewModel, I am unable to access it, and I encounter the following error:
Error;
Note 1
On the other hand, when I try to access the ViewModel using the following method, I don't encounter any issues:
private val viewModel: MyViewModel by activityViewModels()
Note 2
To help better understand the issue, I am attaching the relevant ViewModel, Fragment, and Hilt module files to this report.
AppModule.kt
MyFragment.kt
MyViewModel.kt
Repository.kt
RepositoryImpl.kt
MainActivity.kt
HiltTutorialApplication.kt
build.gradle (app)
build.gradle (project)
AndroidManifest.xml
The text was updated successfully, but these errors were encountered: