You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio编译报错:参数类型需为@Entity注解类或其集合

Room编译错误:参数类型必须是@Entity注解的类或其集合/数组

在Android Studio开发简易笔记应用,已完成Room数据库的Entity、DAO及数据库类搭建,但编译时出现如下错误:

error: Type of the parameter must be a class annotated with @Entity or a collection/array of it. kotlin.coroutines.Continuation<? super java.lang.Long> $completion);

NotesDao代码

@Dao
interface NotesDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertNote(note: Notes): Long

    @Update
    suspend fun updateNote(note: Notes): Int

    @Delete
    suspend fun deleteNote(note: Notes): Int

    @Query("SELECT * FROM notes_data_table")
    fun getAllNotes(): Flow<List<Notes>>
} 

Notes实体代码

@Entity(tableName = "notes_data_table")
data class Notes(

    @PrimaryKey(autoGenerate = true)
    val id:Int? = null,

    @ColumnInfo(name = "note_title")
    val title:String,

    @ColumnInfo(name = "note_body")
    val body:String
)

依赖配置

dependencies {
    def room_version = "2.5.1"
    def lifecycle_version = "2.6.1"
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"

    // optional - Kotlin Extensions and Coroutines support for Room
    implementation "androidx.room:room-ktx:$room_version"

    //coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1'

    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    // LiveData
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"

    //ssp, sdp
    implementation 'com.intuit.ssp:ssp-android:1.1.0'
    implementation 'com.intuit.sdp:sdp-android:1.1.0'

    implementation 'androidx.core:core-ktx:1.10.1'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:<version>'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

数据库类代码

@Database(entities = [Notes::class], version = 1)
abstract class NotesDB : RoomDatabase() {
    abstract val notesDAO: NotesDao
 
    companion object {
        @Volatile
        private var INSTANCE: NotesDB? = null
        fun getInstance(context: Context): NotesDB {
            synchronized(this) {
                var instance = INSTANCE
                if (instance == null) {
                    instance = Room.databaseBuilder(
                        context.applicationContext,
                        NotesDB::class.java,
                        "notes_data_database"
                    ).build()
                }
                return instance
            }
        }
    }
}

补充说明

此前有开发者通过升级Kotlin或Room版本解决类似问题,但本人使用最新版本仍报错,尝试降级版本后问题依然存在。

内容的提问来源于stack exchange,提问作者wise394

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 04:32:47