0

First of all this is not a duplicate question, I have tried all the other methods found here on SO but they didn't solve my problem. I'm having a problem with the ViewModelProviders. These are my codes.

viewModel = ViewModelProviders.of(this).get(VideoPlayerViewModel::class.java)

The 'this' is giving an error. Here's the error

None of the following functions can be called with the arguments supplied.
of(Fragment) defined in androidx.lifecycle.ViewModelProviders
of(FragmentActivity) defined in androidx.lifecycle.ViewModelProviders

This is my class

class VideoPlayerActivity : Activity() {

    private lateinit var viewModel: VideoPlayerViewModel
}

As I said I have read some other answers regarding to this problem and they are telling me to extend AppCompatActivity() instead of Activity(). The problem is when i change to AppCompatActivity() it does solve the error but gives a whole lot of error in my other codes. So i want to do it without changing the Activity(). What is the solution?

Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29
Junior
  • 170
  • 2
  • 10
  • 1
    see that answer https://stackoverflow.com/a/52811933/4854891 – Jakir Hossain Nov 20 '19 at 09:55
  • 2
    `Activity` is raw system implementation you shouldn't extend directly (in most cases anyway). `AppCompatActivity` is androidx library extension of `Activity` which you should use as it implements most functionality or hooks for androidx to work properly like `ViewModelProvider` in your case. – Pawel Nov 20 '19 at 10:21
  • Possible duplicate of [Kotlin does not understand ViewModelProviders.of(activity ?: fragment)](https://stackoverflow.com/questions/52811685/kotlin-does-not-understand-viewmodelproviders-ofactivity-fragment) – C-Shark Nov 20 '19 at 21:45

0 Answers0