Chapter 4 Adding a view model

According to Listing 4.3 I added the line:

private val quizViewModel: QuizViewModel by viewModels()

but QuizViewModel is coloured red, and show the msg: “unresolved reference”.
Furthermore viewModels() is underlined.

I made the ViewModel class according to Listing 4.2 and I added the depencies as shown in listing 4.1
Also I added “import androidx.activity.viewModels” in the MainActivity.kt file.

I am using Android Studio Hedgehog.

What can be wrong ?

Look at the section titled “Build issues” at the end of Chapter 5. Try those things and see if they fix the problem. If none of those things work, can you share the contents of both build.gradle files?

Hi Brian,\

Thank you for your suggestions.

Now I found the cause of the problem.

In Listing 4.2 the line: package com.bignerdranch.android.geoquiz

was missing, so that the Mainactivity.kt file couldn’t find the class name.

Regards,

Hans Schell