Я использую пространство для сохранения в своем приложении для Android, и как только я создаю свой класс базы данных, я получаю следующие ошибки:
- ошибка: класс сущности должен быть аннотирован
@Entity
- ошибка: сущности и Pojos должны иметь открытый конструктор.У вас может быть пустой конструктор или конструктор, параметры которого совпадают с полями (по имени и типу).
- ошибка: у сущности должно быть хотя бы 1 поле, помеченное
@PrimaryKey
Я сузил его до своего класса Базы данных, исключив все остальное, кроме моего класса Entity и DAO
.Я перепробовал все, что нашел в сети, но ничего не получилось.
Вот класс Entity:
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'
}
Я действительно не могу понять, где я ошибся.Я перешел по этой ссылке , чтобы все настроить.
Вся помощь оценена!