房间错误:参数的类型必须是使用 @Entity 注释的类或其集合/数组。即使添加了 androidx.room:room-ktx
Posted
技术标签:
【中文标题】房间错误:参数的类型必须是使用 @Entity 注释的类或其集合/数组。即使添加了 androidx.room:room-ktx【英文标题】:Room error: Type of the parameter must be a class annotated with @Entity or a collection/array of it. even after adding androidx.room:room-ktx 【发布时间】:2022-01-11 09:45:49 【问题描述】:根据我对 “suspend” 关键字的理解。 但是我已经添加了 implementation("androidx.room:room-ktx:$roomVersion") 依赖
Gradle 文件是:
plugins
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
android
compileSdk 31
defaultConfig
applicationId "com.kaushik.roomdemo"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildTypes
release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
buildFeatures
viewBinding true
compileOptions
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
kotlinOptions
jvmTarget = '1.8'
dependencies
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
def lifecycle_version = "2.4.0"
def roomVersion = "2.3.0"
// ViewModel
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")
// LiveData
implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")
// Annotation processor
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
// Room
implementation("androidx.room:room-runtime:$roomVersion")
kapt("androidx.room:room-compiler:$roomVersion")
// Kotlin Extensions and Coroutines support for Room
implementation("androidx.room:room-ktx:$roomVersion")
// Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0-RC'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0-RC'
// Activity KTX for viewModels()
implementation "androidx.activity:activity-ktx:1.4.0"
实体是:
@Entity(tableName = "subscriber_table")
data class Subscriber(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name="subscriber_id")
val id:Int,
@ColumnInfo(name="subscriber_name")
val name:String,
@ColumnInfo(name="subscriber_email")
val email:String
)
道是:
@Dao
interface SubscriberDao
@Query("SELECT * from subscriber_table")
fun getAllSubscriber(): LiveData<List<Subscriber>>
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addSubscriber(subscriber: Subscriber):Long
@Delete
suspend fun deleteSubscriber(subscriber: Subscriber)
数据库是:
@Database(entities = [Subscriber::class], version = 1)
abstract class SubscriberDataBase:RoomDatabase()
abstract fun subscriberDao(): SubscriberDao
companion object
@Volatile
private var INSTANCE: SubscriberDataBase? = null
fun getInstance(context: Context):SubscriberDataBase
synchronized(this)
var instance:SubscriberDataBase? = INSTANCE
if(instance == null)
instance = Room.databaseBuilder(
context.applicationContext,
SubscriberDataBase::class.java,
"Subscriber_DataBase"
).build()
return instance
现在每次我尝试构建代码时都会引发以下错误: D:\CODE IT\Android Developmeent\RoomDemo\app\build\tmp\kapt3\stubs\debug\com\kaushik\roomdemo\db\SubscriberDao.java:17: error: Type of the parameter must be a class用 @Entity 或它的集合/数组注释。 kotlin.coroutines.Continuation super java.lang.Long> 续);
我已经在依赖文件中添加了实现("androidx.room:room-ktx:$roomVersion")。 还是不行。任何人,请帮助!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
【问题讨论】:
【参考方案1】:您的代码看起来不错,您可能尚未在 RoomDatabase()
类中定义您的实体。
@Database(
entities = [Subscriber::class],
version = 1,
exportSchema = false
)
abstract class AppDatabase : RoomDatabase()
//...
【讨论】:
先生,我已经添加了数据库,我已经编辑了帖子以预览数据库。请再次审查它。我被困在里面了【参考方案2】:试试这个..也许它会帮助你
@Dao
interface SubscriberDao
@Query("SELECT * from subscriber_table")
fun getAllSubscriber(): LiveData<List<Subscriber>>
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun addSubscriber(subscriber: Subscriber):Long
@Delete
fun deleteSubscriber(subscriber: Subscriber)
【讨论】:
您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。以上是关于房间错误:参数的类型必须是使用 @Entity 注释的类或其集合/数组。即使添加了 androidx.room:room-ktx的主要内容,如果未能解决你的问题,请参考以下文章
房间持久性:错误:实体和 Pojos 必须有一个可用的公共构造函数
参数的类型必须是带有 @Entity 注释的类或其集合/数组
android开发出现以下错误:EntityUtils.consume(entity1),提示方法consume()不存在???