1

Im using this kotlin Room project and trying to instantiate "viewModel" that needs a Dao parameter https://github.com/googlesamples/android-architecture-components/tree/master/BasicRxJavaSampleKotlin/app/src/main/java/com/example/android/observability

the project uses this line to to it:

private val viewModel: UserViewModel by viewModels()

but i cant get viewModels() to resolve. i noticed the project has this import

import androidx.activity.viewModels

but "activity" is red and not resolving.

i have another viewmodel B with no parameter that is instantiated by

val model = ViewModelProviders.of(this)[MyViewModel::class.java]

i am trying to figure out why the import is red for me

also is it possible to instantiate a viewmodel with parameters in the second way of instantiating?

please let me know if i need to include other files to determine.

DevJZ
  • 100
  • 8
  • 1
    i added implementation 'androidx.activity:activity-ktx:1.1.0-alpha02' dependency and now the import resolves. the original project does not have that dependency. im wondering whats going on and now that androidx.activity import resolves for me, the viewModels() now errors but gives message "none of the following candidates is applicable because of receiver type mismatch" does someone have an idea of whats going on? – DevJZ Aug 16 '19 at 22:21
  • Well done resolving the dependency. See [this](https://stackoverflow.com/a/48988779/2376004) if you have problems compiling. – Bad Loser Oct 30 '19 at 00:43

0 Answers0