Chapter 16 Saving Bookmarks with Room

After I implemented the class MapsViewModel the application always crashed after the installing process.

The error is java.lang.RuntimeException: Cannot create an instance of the class …placebook.viewmodel.MapsViewModel at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:207).

I thought it was my fault, but I compile the final version of the Chapter 16, and I got the same error, then I compile the final versión of the Chapter 17, 18 and 19, and I got the same error.

I am using the Android Studio 3.2, Kotlin plugin 1.2.71 and targetSdkVersion 28 (Oreo 8.1).

I found that the application crashed when it executes private var bookmarkRepo: BookmarkRepo = BookmarkRepo(getApplication()).

I will continue with the research.

Thanks for any help.

@tblank Can you please help with this when you get a chance? Thank you - much appreciated! :]

1 Like

You should be able to run the app if you change the Kotlin plugin version to 1.2.21. This is the version we tested with when writing the book. Please try this and let me know.

It appears that the main issue is that annotation processing is handled differently when updating to versions of Kotlin 1.3 and higher. The fix will likely require some changes to the Gradle settings and possibly updates to some annotations in the database classes. I will look into providing some instructions for using newer Kotlin versions once I’ve had a chance to test the changes.

Thank you,
Tom

1 Like

I had the same problem and I solved it adding:

apply plugin: ‘kotlin-kapt’ on top of the buidl.gradle file.

I found the answer here: android - Cannot create an instance of class ViewModel - Stack Overflow

Thanks so much! it works :love_you_gesture: