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
相关产品推荐
相关产品推荐

