4

我是 android 开发的新手,我正在尝试通过遵循 android 架构组件来制作一个笔记应用程序,但是在运行时我的 DAO 中出现错误,如果有人可以提供帮助,我将不胜感激。这是我得到的代码和错误。

我得到的错误

道:-

'''

@Dao
interface NoteDao {
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insert(note :Note)

    @Delete
    suspend fun delete(note : Note)

    @Query("SELECT * FROM Notes_table order by id")
    fun getALL(): LiveData<List<Note>>

    @Query("SELECT * From Notes_table where id= :pos")
    fun getSpecific(pos :Int):Note

}

'''

实体:-

'''

@Entity(tableName = "Notes_table")
data class Note(@ColumnInfo(name="noteText") val text:String) {
    @PrimaryKey(autoGenerate = true) var id:Int =0

}

''' 数据库:-

'''

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

    abstract fun getNoteDao():NoteDao

    companion object{
        @Volatile
        private var Instance: NoteDatabase?=null

        fun getDatabase(context :Context):NoteDatabase{

            return Instance ?: synchronized(this){
                val instance=Room.databaseBuilder(context.applicationContext,
                NoteDatabase::class.java,"note_database").build()
                Instance=instance
                instance
            }
        }

    }

}

DAO 的导入:-

import androidx.lifecycle.LiveData
import androidx.room.

实体进口:-

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

数据库导入:-

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

如果需要,我可以提供其余代码。

4

3 回答 3

4

在评论部分的帮助下,通过阅读文档,我发现我的 DAO 出了点问题,最终这与我添加到依赖项中的ROOM版本有关,我刚刚更新了这些,现在它工作正常。

于 2021-06-10T11:21:01.710 回答
3

今天遇到了同样的问题,通过更新Room版本来2.3.0解决2.4.0-beta02。版本可以在这里找到:https ://developer.android.com/jetpack/androidx/releases/room#2.3.0-alpha04

于 2021-11-18T20:40:13.027 回答
2

通过将房间版本提高到“2.4.0-beta01”来修复

于 2021-11-06T08:47:57.733 回答