Anthony Porter
Anthony Porter

Reputation: 152

ViewModelProviders.of(FragmentActivity).get(ViewModel::class.java) asking for activity instead of viewmodel in get() call

ViewModelProviders.of(this).get() is failing for unforeseen reasons This is being called in my main activity which inherits from FragmentActivity. I'm actually getting an error on the .get() call. The error says it's expecting an activity instead of a ViewModel which goes against the docs. I've cleared and invalidated caches. My guess is that I have a bad import or an old library version somehow

import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.databinding.DataBindingUtil import androidx.lifecycle.ViewModelProviders import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.apsoftware.urbandictionary.R

class DefinitionActivity : AppCompatActivity() {

    private lateinit var binding: ActivityPostListBinding
    private lateinit var viewModel: DefinitionActivity


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        binding.postList.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        viewModel = ViewModelProviders.of(this).get(DefinitionViewModel::class.java)
        binding.viewModel = viewModel
    } }

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
implementation 'androidx.core:core-ktx:1.1.0-alpha04'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
implementation "com.squareup.okhttp3:okhttp:3.12.0"
implementation "com.squareup.retrofit2:retrofit:2.5.0"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.4.0"
implementation 'com.squareup.retrofit2:converter-moshi:2.5.0'
implementation 'com.squareup.moshi:moshi:1.6.0'
kapt 'com.android.databinding:compiler:3.1.4'
implementation "com.google.dagger:dagger:2.20"
kapt "com.google.dagger:dagger-compiler:2.11"
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
compileOnly "org.glassfish:javax.annotation:3.1.1"
implementation "io.reactivex.rxjava2:rxjava:2.2.2"
implementation "io.reactivex.rxjava2:rxandroid:2.0.2"
implementation "android.arch.lifecycle:extensions:1.1.1"

Upvotes: 1

Views: 928

Answers (2)

Kishore Jethava
Kishore Jethava

Reputation: 6834

Change

 private lateinit var viewModel: DefinitionActivity

to

 private lateinit var viewModel: DefinitionViewModel

Upvotes: 1

ianhanniballake
ianhanniballake

Reputation: 199805

You've declared

private lateinit var viewModel: DefinitionActivity

I.e., viewModel is an instance of a DefinitionActivity. It should be a DefinitionViewModel to match what you pass into get().

Upvotes: 3

Related Questions