google-developer-training / android-basics-kotlin-cupcake-app

Apache License 2.0
103 stars 163 forks source link

Android Basics: Shared ViewModel #45

Closed thatwaz closed 2 years ago

thatwaz commented 3 years ago

URL of codelab https://github.com/google-developer-training/android-basics-kotlin-cupcake-app.git

In which task and step of the codelab can this issue be found? StartFragment.kt

Describe the problem private val sharedViewModel: OrderViewModel by activityViewModels() After inserting this line of code into each fragment as instructed, Android Studio does not recognize the activityViewModels() even after import. The error message reads:

Type mismatch: inferred type is ViewModel but OrderViewModel was expected

Steps to reproduce?

  1. Go to...
  2. Click on...
  3. See error...

Versions Android Studio version: arctic fox 2020.3.1 Patch 3 API version of the emulator: 28

Additional information Include screenshots if they would be useful in clarifying the problem.

image image

thatwaz commented 2 years ago

i figured it out. The order View Model class was not extended from ViewModel

This fixed it

class OrderViewModel : ViewModel()