Home > Mobile >  I can't give fragment as argument inside ViewModelProviders.of() function
I can't give fragment as argument inside ViewModelProviders.of() function

Time:12-01

I want to define ViewModel in fragment but the function is not working properly.

I created a ViewModel class as FeedViewModel . And now I want to define it in fragment but the function is not working properly.I checked out the gradle file but can't find the solution. of function is wrong

Error Message :None of the following functions can be called with the arguments supplied. of(Fragment) defined in android.arch.lifecycle.ViewModelProviders of(FragmentActivity) defined in android.arch.lifecycle.ViewModelProviders


private lateinit var viewModel : FeedViewModel

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    
    viewModel=ViewModelProviders.of(this).get(FeedViewModel::class.java)
  //  viewModel.refreshData()


}

Image

CodePudding user response:

Try:

viewModel = ViewModelProvider(this)[FeedViewModel::class.java]
  • Related