我试图在我的应用中使用Realm,但没有成功。问题似乎是我在同一个应用程序中使用数据绑定和Realm。
objectif,让用户使用View输入我通过数据绑定获得的一些数据,并插入到Realm数据库中
我有以下错误:
Character is not part of the schema for this Realm
at io.realm.internal.modules.CompositeMediator.getMediator(CompositeMediator.java:172)
at io.realm.internal.modules.CompositeMediator.getTableName(CompositeMediator.java:90)
at io.realm.RealmSchema.getTable(RealmSchema.java:296)
at io.realm.Realm.createObjectInternal(Realm.java:817)
at io.realm.Realm.createObject(Realm.java:801)
这是我的角色宣言
@RealmClass
public class Character extends RealmObject implements Serializable
private String classGame;
private String name;
private RealmList<Item> equipments;
private RealmList<CaracteristicClass> caracteristicClasses;
private RealmList<Caracteristic> parchemin;
private RealmList<Caracteristic> caracteristics;
private RealmList<Sort> spells;
private int nbCaracteristicPointAvailable;
private ObservableField<String> nbCaracteristicPointAvailableStringOb;
private int level;
private String levelString;
public Character() {
initCharacter();
}
private void initCharacter(){
nbCaracteristicPointAvailableStringOb = new ObservableField<>();
caracteristics = new RealmList<>();
parchemin = new RealmList<>();
spells = new RealmList<>();
/*
* Init base caracteristic
*/
Caracteristic caracteristic = new Caracteristic(TypeCaracteristic.VITALITE.toString(),0,0,11,0);
caracteristics.add(caracteristic);
caracteristic = new Caracteristic(TypeCaracteristic.SAGESSE.toString(),0,0,12,0);
caracteristics.add(caracteristic);
caracteristic = new Caracteristic(TypeCaracteristic.FORCE.toString(),0,0,13,0);
caracteristics.add(caracteristic);
caracteristic = new Caracteristic(TypeCaracteristic.INTELLIGENCE.toString(),0,0,14,0);
caracteristics.add(caracteristic);
caracteristic = new Caracteristic(TypeCaracteristic.CHANCE.toString(),0,0,15,0);
caracteristics.add(caracteristic);
caracteristic = new Caracteristic(TypeCaracteristic.AGILITE.toString(),0,0,16,0);
caracteristics.add(caracteristic);
caracteristic = new Caracteristic(TypeCaracteristic.LEVEL.toString(),1,200,-1,200);
caracteristics.add(caracteristic);
/*
* Parchemin caractéristique
*/
caracteristic = new Caracteristic(TypeCaracteristic.VITALITE.toString(),0,100,11,100);
parchemin.add(caracteristic);
caracteristic = new Caracteristic(TypeCaracteristic.SAGESSE.toString(),0,100,12,100);
parchemin.add(caracteristic);
caracteristic = new Caracteristic(TypeCaracteristic.FORCE.toString(),0,100,13,100);
parchemin.add(caracteristic);
caracteristic = new Caracteristic(TypeCaracteristic.INTELLIGENCE.toString(),0,100,14,100);
parchemin.add(caracteristic);
caracteristic = new Caracteristic(TypeCaracteristic.CHANCE.toString(),0,100,15,100);
parchemin.add(caracteristic);
caracteristic = new Caracteristic(TypeCaracteristic.AGILITE.toString(),0,100,16,100);
parchemin.add(caracteristic);
caracteristicClasses = new RealmList<>();
CaracteristicClass caracteristicClass = new CaracteristicClass(0,TypeCaracteristic.VITALITE.toString(),1);
caracteristicClasses.add(caracteristicClass);
caracteristicClass = new CaracteristicClass(0,TypeCaracteristic.SAGESSE.toString(),3);
caracteristicClasses.add(caracteristicClass);
caracteristicClass = new CaracteristicClass(0,TypeCaracteristic.AGILITE.toString(),1);
caracteristicClasses.add(caracteristicClass);
caracteristicClass = new CaracteristicClass(0,TypeCaracteristic.CHANCE.toString(),1);
caracteristicClasses.add(caracteristicClass);
caracteristicClass = new CaracteristicClass(0,TypeCaracteristic.FORCE.toString(),1);
caracteristicClasses.add(caracteristicClass);
caracteristicClass = new CaracteristicClass(0,TypeCaracteristic.INTELLIGENCE.toString(),1);
caracteristicClasses.add(caracteristicClass);
}
public Character(final String name, final int level ){
initCharacter();
}
public String getClassGame() {
return classGame;
}
public void setClassGame(String classGame) {
this.classGame = classGame;
}
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}
public RealmList<Item> getEquipments() {
return equipments;
}
public void setEquipments(RealmList<Item> equipments) {
this.equipments = equipments;
}
public RealmList<Caracteristic> getParchemin() {
return parchemin;
}
public void setParchemin(RealmList<Caracteristic> parchemin) {
this.parchemin = parchemin;
}
public RealmList<Caracteristic> getCaracteristics() {
return caracteristics;
}
public void setCaracteristics(RealmList<Caracteristic> caracteristics) {
this.caracteristics = caracteristics;
}
public RealmList<Sort> getSpells() {
return spells;
}
public void setSpells(RealmList<Sort> spells) {
this.spells = spells;
}
public int getLevel() {
return level;
}
public String getLevelString() {
return levelString;
}
public ObservableField<String> getNbCaracteristicPointAvailableStringOb() {
return nbCaracteristicPointAvailableStringOb;
}
public void setNbCaracteristicPointAvailableStringOb(String nbCaracteristicPointAvailableStringOb) {
this.nbCaracteristicPointAvailableStringOb.set(nbCaracteristicPointAvailableStringOb);
this.nbCaracteristicPointAvailableStringOb.notifyPropertyChanged(BR.character);
}
public void setLevelString(String levelString) {
this.levelString = levelString;
try{
if(!this.levelString.equals("")){
this.level = Integer.parseInt(this.levelString);
if(level>0 && level<=Constants.MAX_LEVEL){
nbCaracteristicPointAvailable = (level -1) * 5;
this.setNbCaracteristicPointAvailableStringOb(String.valueOf(nbCaracteristicPointAvailable));
}
}
}catch (NumberFormatException ex){
Log.i("Character","emptyString or Invalid");
}
}
这是我的模块build.gradle
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'com.jakewharton.butterknife'
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "*******"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "0.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding{
enabled = true;
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso- core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:design:25.0.1'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:recyclerview-v7:25.0.1'
compile 'com.android.support:cardview-v7:25.0.1'
compile 'com.android.support:support-v4:25.0.1'
compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.3'
compile 'com.github.amlcurran.showcaseview:library:5.0.0'
compile 'org.jsoup:jsoup:1.7.3'
compile 'com.android.support:design:25.0.1'
compile 'com.google.android.gms:play-services-appindexing:9.8.0'
compile 'com.jakewharton:butterknife:8.4.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
compile 'org.apache.commons:commons-collections4:4.1'
compile 'org.apache.commons:commons-lang3:3.4'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
testCompile 'junit:junit:4.12'
compile project(path: ':androidhttprequestlibrairy')
}
这是我的项目build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0-alpha1'
classpath "io.realm:realm-gradle-plugin:2.2.1"
classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
这是我使用Realm和DataBinding的活动
final ActivityCreateBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_create);
ButterKnife.bind(binding.getRoot());
Realm.init(getApplicationContext());
RealmConfiguration config = new RealmConfiguration.Builder().build();
realm = Realm.getInstance(config);
realm.beginTransaction();
character = realm.createObject(Character.class);
binding.setCharacter(character);
我在侦听器中的某些代码行使用
realm.commitTransaction();
我不明白这是什么问题......
此致
答案 0 :(得分:1)
来自文档:
字段类型
Realm支持以下字段类型:boolean,byte,short,int,long,float,double,String,Date和byte []。整数类型byte,short,int和long都映射到Realm中的相同类型(实际上很长)。
此外,支持
RealmObject
和RealmList<? extends RealmObject>
的子类来建模关系。
嗯,你也有这个:
private ObservableField<String> nbCaracteristicPointAvailableStringOb;
这不是受支持的RealmModel字段,因此不会起作用。您必须添加@Ignore
注释。
@RealmClass
public class Character extends RealmObject implements Serializable
//...
@Ignore
private ObservableField<String> nbCaracteristicPointAvailableStringOb;
此外,您可以测试这是否可行,但我认为使用静态工厂方法更安全,而不是在也使用的默认构造函数中初始化所有这些列表和内容实例化托管代理时。
public static Character create() {
Character character = new Character();
character.initCharacter();
return character;
}
(在完全不相关的旁注中,我不知道为什么这个类是Serializable
,你为什么要通过ObjectOutputStream抛出一个RealmObject?)
编辑:你也可以尝试通过添加android-apt
插件来强制它工作。
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0-alpha1'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath "io.realm:realm-gradle-plugin:2.2.1"
}
}
然后
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'realm-android'
//...
compile 'com.jakewharton:butterknife:8.4.0'
// ...
apt 'com.jakewharton:butterknife-compiler:8.4.0'