简体   繁体   中英

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

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

Try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM