房间错误-如何解决?

时间:2018-10-17 11:05:42

标签: android android-room

我正在为我的android应用程序保留空间,一旦创建数据库类,就会出现以下错误:

  • 错误:实体类必须用@Entity注释
  • 错误:实体和Pojos必须具有可用的公共构造函数。您可以有一个空的构造函数或一个其参数与字段匹配的构造函数(按名称和类型)。
  • 错误:实体必须至少具有1个用@PrimaryKey注释的字段

我将其范围缩小到我的Database类,从而消除了除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'
}

我真的找不到我哪里出了问题。我按照这个link进行了设置。

感谢所有帮助!

3 个答案:

答案 0 :(得分:0)

也许我的意见可以帮助您:)

实体:

  1. 列表项

缺少表名:

@Entity(tableName = "your_tabel_name_here")
  1. 列表项

无列信息的

public int inventoryId;

public int spellBookId;
  1. 列表项

忽略除空构造函数以外的所有内容

@Ignore

public Character(int characterId, String name, String race ...)

** DAO:**

  1. 首先,将Dao命名为实体:CharacterDAO
  2. 使用以下插入样式:

    @道 公共接口CharacterDAO {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    void insertCharacter(Character... character);
    

    }

**应用数据库:**

@Database(entities = {Character.class},version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {

    private static AppDatabase INSTANCE;

    public abstract CharacterDAO characterDAO();
private static String DATABASE_NAME = "your_database_name";

    //https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.html
    public static synchronized AppDatabase getAppDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DATABASE_NAME)
                            .build();
        }
        return INSTANCE;
    }

答案 1 :(得分:0)

一切都很好。但是我想是,您可能正在导入其他类,而不是@EntityDAO类中的AppDatabase字符。

很少有编码技巧。

  • 如果您要在@ColumnInfo中定义与字段名称相同的名称,则不需要@ColumnInfo注释。
  • 如果您有public变量,则不必具有设置方法和获取方法

答案 2 :(得分:0)

您必须检查与 ROOM 相关的所有类 [@Entity, @Dao, @Database] 必须
保存在同一个包中。有时您需要从资源管理器中手动删除应用文件夹中的构建
以重建代码。