1

Using Jetpack Compose to build small Room database app. I keep getting error:

Cannot create an instance of class com.learning.kotlinreadexstingroomdb.MainViewModel
...
Caused by: java.lang.InstantiationException: java.lang.Class<com.learning.kotlinreadexstingroomdb.MainViewModel> has no zero argument constructor
        at java.lang.Class.newInstance(Native Method)

MainActivity.kt

package com.learning.kotlinreadexstingroomdb

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import com.learning.kotlinreadexstingroomdb.ui.theme.KotlinReadExstingRoomDBTheme

class MainActivity : ComponentActivity() {

    private val mainViewModel: MainViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            KotlinReadExstingRoomDBTheme {
                MyApp()
            }
        }
    }

    @Composable
    fun MyApp() {
        val result by mainViewModel.readAll.collectAsState(initial= emptyList())

        Column(
            modifier = androidx.compose.ui.Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            if (result.isNotEmpty()) {
                Text(
                    //text = person.name,
                    text = "There is Data!",
                    fontSize = MaterialTheme.typography.h4.fontSize
                )
            } else {
                Text(
                    text = "Empty Database",
                    fontSize = MaterialTheme.typography.h4.fontSize
                )
            }
        }

    }
}

MainViewModel.kt

package com.learning.kotlinreadexstingroomdb

import androidx.lifecycle.ViewModel
import com.learning.kotlinreadexstingroomdb.data.PersonRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class MainViewModel
@Inject
constructor(personRepository: PersonRepository): ViewModel(){
    val readAll = personRepository.readAll
}

build.gradle (:app)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'                                            
}
...
dependencies {

    def room_version = "2.4.1"                                              
    implementation("androidx.room:room-runtime:$room_version")              
    annotationProcessor("androidx.room:room-compiler:$room_version")        

    def dagger_hilt_version = "2.40.5"
    implementation "com.google.dagger:hilt-android:$dagger_hilt_version"    
    kapt "com.google.dagger:hilt-compiler:$dagger_hilt_version"             

    def compose_version = "1.0.5"                                           
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
    implementation 'androidx.activity:activity-compose:1.4.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"

}

I feel like I've tried everything I could find on the web, but nothing fixes it.

Note: Code is based on: Pre-Populate ROOM Database with Already Loaded Data | Android Studio Tutorial

2
  • 2
    You seems using Hilt, your activity should be annotated with @dagger.hilt.android.AndroidEntryPoint, also you need to have application class annotated with @dagger.hilt.android.HiltAndroidApp. More about Hilt integration can be found in documentation Commented Jan 20, 2022 at 2:57
  • @PhilipDukhov I added @dagger.hilt.android.AndroidEntryPoint but that doesn't fix it. I like the idea of putting @HiltAndroidApp somewhere, but I'm not sure I understand where? Thanks!
    – SqueezeOJ
    Commented Jan 20, 2022 at 16:33

2 Answers 2

2

Make sure you annotate your Application class to initialise Hilt

@HiltAndroidApp
class App : Application()

Your Application class needs to be registered in the manifest if it isn't already.

<application
        android:name=".App"
        ... />

You also need to annotate your activities and fragments so that Hilt can inject into them.

@AndroidEntryPoint
class MainActivity
3
  • I like where you're going with this, but when I change private val... to hiltViewModel(), it highlights hiltViewModel in red and says Unresolved reference: hiltViewModel. What am I missing?
    – SqueezeOJ
    Commented Jan 20, 2022 at 16:30
  • 1
    @SqueezeOJ I've updated my answer to remove hiltViewModel() as that's only when using compose, I've also added some other things to check. Commented Jan 20, 2022 at 17:28
  • I hate to admit it, but this may still be over my head! I'm going to back-up a bit and try a simpler non-hilt Room database.
    – SqueezeOJ
    Commented Jan 20, 2022 at 21:34
1

Add @AndroidEntryPoint annotation in main activity

@AndroidEntryPoint // this line 
class MainActivity : ComponentActivity() {

    private val mainViewModel: MainViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            KotlinReadExstingRoomDBTheme {
                MyApp()
            }
        }
    }
1
  • I tried this and get the following error: public final class MainActivity extends androidx.activity.ComponentActivity { ^ Expected @AndroidEntryPoint to have a value. Did you forget to apply the Gradle Plugin? (dagger.hilt.android.plugin)
    – SqueezeOJ
    Commented Jan 20, 2022 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.