Cannot create an instance of class ViewModel

后端 未结 23 2679
[愿得一人]
[愿得一人] 2020-12-05 12:37

I am trying to write a sample app using Android architecture components and but even after trying for days I could not get it to work. It gives me the above exception.

23条回答
  •  一生所求
    2020-12-05 12:59

    Please add below code. It worked for me

    val binding = FragmentLayoutBinding.inflate(inflater, container, false)
    
    val viewModel = ViewModelProvider(
                requireActivity(),
                defaultViewModelProviderFactory
                ).get(MainViewModel::class.java)
    

提交回复
热议问题