Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
private val viewModelProvider by lazy { ViewModelProvider(requireActivity()) }
private val myViewModel by lazy {
viewModelProvider.get(
ViewModel::class.java
The error is:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.net2software.mobile.mpos_cirebon.debug, PID: 15298
java.lang.RuntimeException: Cannot create an instance of class com.mypackagename.viewmodel.viewmodelClass
How can I fix this?
You can use Fragment Extension
def fragment_version = "1.4.0"
implementation "androidx.fragment:fragment-ktx:$fragment_version"
Fragment View Model
private val viewModel by viewModels<ViewModel>()
Activity Shared ViewModel between fragments
private val activityViewModel by activityViewModels<ViewModel>()
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.