【发布时间】:2019-03-22 01:05:12
【问题描述】:
我在我的 android 应用程序上使用了持久性空间,一旦我创建了我的数据库类,我就会收到以下错误:
- 错误:实体类必须用
@Entity注解 - 错误:实体和 Pojo 必须有一个可用的公共构造函数。您可以有一个空的构造函数或一个参数与字段匹配的构造函数(按名称和类型)。
- 错误:实体必须至少有 1 个用
@PrimaryKey注释的字段
我将其缩小到我的数据库类,我消除了我的实体和DAO 类以外的所有其他内容。我已经尝试了我在网上找到的所有内容,但没有任何效果。
这是实体类:
import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.ForeignKey;
import android.arch.persistence.room.PrimaryKey;
import android.content.pm.PackageManager;
import static android.arch.persistence.room.ForeignKey.CASCADE;
@Entity
public class Character {
@PrimaryKey(autoGenerate = true)
public int characterId;
@ColumnInfo(name = "name")
public String name;
@ColumnInfo(name = "race")
public String race;
@ColumnInfo(name = "occupation")
public String occupation;
@ColumnInfo(name = "shadow")
public String shadow;
@ColumnInfo(name = "accurate")
public int accurate;
@ColumnInfo(name = "cunning")
public int cunning;
@ColumnInfo(name = "discreet")
public int discreet;
@ColumnInfo(name = "persuasive")
public int persuasive;
@ColumnInfo(name = "quick")
public int quick;
@ColumnInfo(name = "resolute")
public int resolute;
@ColumnInfo(name = "strong")
public int strong;
@ColumnInfo(name = "vigilant")
public int vigilant;
@ColumnInfo(name = "toughness")
public int toughness;
@ColumnInfo(name = "painThreshold")
public int painThreshold;
@ColumnInfo(name = "corruptionThreshold")
public int corruptionThreshold;
@ColumnInfo(name = "defense")
public int defense;
public int inventoryId;
public int spellBookId;
public Character() {
}
public Character(int characterId, String name, String race, String occupation, String shadow, int accurate, int cunning, int discreet, int persuasive, int quick, int resolute, int strong, int vigilant, int toughness, int painThreshold, int corruptionThreshold, int defense, int inventoryId, int spellBookId) {
this.characterId = characterId;
this.name = name;
this.race = race;
this.occupation = occupation;
this.shadow = shadow;
this.accurate = accurate;
this.cunning = cunning;
this.discreet = discreet;
this.persuasive = persuasive;
this.quick = quick;
this.resolute = resolute;
this.strong = strong;
this.vigilant = vigilant;
this.toughness = toughness;
this.painThreshold = painThreshold;
this.corruptionThreshold = corruptionThreshold;
this.defense = defense;
this.inventoryId = inventoryId;
this.spellBookId = spellBookId;
}
...getters and setters
这是数据库类:
@Database (entities = {Character.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract CharDao charDao();
}
我的 DAO 类:
@Dao
public interface CharDao {
@Insert
void addChar (Character character);
}
Build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "symbhero.studio.com.symbhero"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'android.arch.persistence.room:runtime:1.1.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.0'
}
我真的不知道我哪里出错了。我按照link 进行了所有设置。
感谢所有帮助!
【问题讨论】:
-
你必须创建两个没有 PrimaryKey 的构造函数
-
你的道课在哪里?
-
您尝试过清理项目并重建项目吗?
-
我现在放了 DAO。我试图清理和重建,没有运气。
标签: android android-room