2

I'm new to Android was following the code to create a Room Database in Kotlin through the official Android Documentation . [1]: https://developer.android.com/codelabs/android-room-with-a-view-kotlin#7

MY Database Class-

package com.example.android.notesapp

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = arrayOf(Note::class), version = 1, exportSchema = false)
 abstract class NoteDatabase : RoomDatabase() {

    abstract fun noteDao(): NoteDao

    companion object {
        // Singleton prevents multiple instances of database opening at the
        // same time.
      @Volatile
        private var INSTANCE: NoteDatabase? = null

        fun getDatabase(context: Context): NoteDatabase {
            // if the INSTANCE is not null, then return it,
            // if it is, then create the database
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                        context.applicationContext,
                        NoteDatabase::class.java,
                        "note_database"
                ).build()
                INSTANCE = instance
                // return instance
                instance
            }
        }
    }
}

I'm getting the following errors:-

1) Unresolved reference at this line -> return INSTANCE ?: synchronized(this) ,synchronized () 
  error


2) Room.databaseBuilder(
                        context.applicationContext,
                        NoteDatabase::class.java,
                        "note_database") 
 Here it is showing not enough information to infer type variable t  inside .databaseBuilder() and  unresolved reference .java


3)@Volatie annotation is showing unresolved reference .

MY Dao Interface

package com.example.android.notesapp

import androidx.lifecycle.LiveData import androidx.room.*

@Dao interface NoteDao {

@Insert(onConflict = OnConflictStrategy.IGNORE) // In case we don't want same data to be inserted
suspend fun insert(note: Note)                      //coroutines -> background process I/O

@Delete
suspend fun delete(note: Note)

@Query(value = "Select * from notes_table order by id ASC")
fun getAllNotes(): LiveData <List<Note>>

}

My Class for Table -> Note Class


    package com.example.android.notesapp
    
    import androidx.room.ColumnInfo
    import androidx.room.Entity
    import androidx.room.PrimaryKey
    
    @Entity(tableName = "notes_table")
    class Note(@ColumnInfo(name = "text") val text: String) {
    
        @PrimaryKey(autoGenerate = true)
        var id: Int = 0;
    
    
    }

P.S. I copied all the dependencies and their version required from the official android site. [1]: https://developer.android.com/codelabs/android-room-with-a-view-kotlin#3 Please help to get rid of these errors.

2 Answers2

1

I had the some problem. Have changed:implementation of core from 1.6.0 back to 1.5.0

'implementation 'androidx.core:core-ktx:1.5.0'
pawchi
  • 77
  • 7
0
val instance: NoteDatabase = Room.databaseBuilder(
                context.applicationContext,
                NoteDatabase::class.java,
                "note_database"
            ).build()

Define the type of the database for the instance variable. It will resolve the issue.