Tehleel Mir
Tehleel Mir

Reputation: 853

java.lang.RuntimeException: Cannot create an instance of class com.example.mvvmapp.NoteViewModel in koltlin

Error: can't create an instance of the view model class

here is how I'm trying to create it

class MainActivity : AppCompatActivity() {
    lateinit var noteViewModel: NoteViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        noteViewModel = ViewModelProvider(
            this,
            ViewModelProvider.AndroidViewModelFactory.getInstance(this.application)
        ).get(
            NoteViewModel::class.java
        )

        noteViewModel.getAllNotes().observe(this, object : Observer<List<Note>> {
            override fun onChanged(t: List<Note>?) {
                Toast.makeText(this@MainActivity, t.toString(), Toast.LENGTH_LONG).show()
            }
        })
    }
}

And here is my view model class

class NoteViewModel(application: Application) : AndroidViewModel(application) {
    private val repository: NoteRepository = NoteRepository(application)
    private val allNotes: LiveData<List<Note>> = repository.getAllNotes()

    fun insert(note: Note) {
        repository.insert(note)
    }

    fun delete(note: Note) {
        repository.delete(note)
    }

    fun update(note: Note) {
        repository.update(note)
    }

    fun deleteAll() {
        repository.deleteAllNotes()
    }

    fun getAllNotes(): LiveData<List<Note>> = allNotes
}

everything looks fine, I don't know what's causing the error

Upvotes: 1

Views: 2379

Answers (4)

lostPlayer
lostPlayer

Reputation: 81

if anyone is having this problem one solution to fix this is just make ViewModel constructor public (Class that extends AndroidViewModel)

Upvotes: 0

Hossam Sadekk
Hossam Sadekk

Reputation: 125

I had the same problem(in java). This solved it:

// old code

mWordViewModel = new ViewModelProvider(this).get(WordViewModel.class);

// new code

mWordViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(WordViewModel.class);

Upvotes: 0

Dharmender Manral
Dharmender Manral

Reputation: 1520

Try with the below dependency then you will be able to get kotlin property delegate "viewModels()" in your activity.

dependencies {
    val lifecycle_version = "2.3.1"
    val arch_version = "2.1.0"

    // ViewModel
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")
    // LiveData
    implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")
    // Lifecycles only (without ViewModel or LiveData)
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version")

    // Saved state module for ViewModel
    implementation("androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version")

   implementation "androidx.activity:activity-ktx:1.3.0-beta01"

    }


//write below code in your activity for instantiate viewModel
 private val noteViewModel: NoteViewModel by viewModels()


//For more detail go through below link
 https://developer.android.com/jetpack/androidx/releases/lifecycle#declaring_dependencies

Upvotes: 0

Piyush Mamidwar
Piyush Mamidwar

Reputation: 85

You can use the kotlin property delegate "viewModels()" to instantiate your viewmodel

class MainActivity : AppCompatActivity() {
 var noteViewModel: NoteViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    noteViewModel.getAllNotes().observe(this, object : Observer<List<Note>> {
        override fun onChanged(t: List<Note>?) {
            Toast.makeText(this@MainActivity, t.toString(), Toast.LENGTH_LONG).show()
        }
    })
}

}

Upvotes: 1

Related Questions