gpt4 book ai didi

android - Realm 和 Android 数据绑定(bind)

转载 作者:行者123 更新时间:2023-11-30 00:51:40 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序中使用 Realm,但没有成功。问题似乎是我在同一个应用程序中使用数据绑定(bind)和 Realm 。

目标是让用户使用 View 输入我通过数据绑定(bind)获得的一些数据,并插入到 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 的 Activity

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();

我不明白这是什么问题......

此致

最佳答案

来自文档:

Field types

Realm supports the following field types: boolean, byte, short, int, long, float, double, String, Date and byte[]. The integer types byte, short, int, and long are all mapped to the same type (long actually) within Realm.

Moreover, subclasses of RealmObject and RealmList<? extends RealmObject> are supported to model relationships.

好吧,你还有这个:

 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'

关于android - Realm 和 Android 数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40940253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com