2016-12-02 151 views
1

我想在我的應用程序中使用Realm,但沒有成功。這個問題似乎是我在同一個應用程序中使用數據綁定和Realm的事實。Realm和Android DataBinding

的OBJECTIF,更是讓用戶使用搜索輸入一些數據,我有數據綁定得到的,並插入到一個境界數據庫

我有以下錯誤:

​​

這裏我的性格聲明

@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 
    } 

這裏我的活動,使用領域和數據綁定

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

我沒有understant是什麼問題...

真誠,

+0

請發表您的完整Character.class – Farasy

+0

@Farasy:Character類增加 – kevingiroux

+0

數據綁定似乎並不成爲問題。它適用於我的測試場景。檢查這個線程,可以幫助:https://github.com/realm/realm-java/issues/1368 – Farasy

回答

1

從文檔:

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' 
+0

對不起,它不工作,我添加@Ignore,但沒有任何改變。 – kevingiroux

+0

我們也可以嘗試強制它使用'android-apt'插件 – EpicPandaForce

+0

現在我有一個生成問題:錯誤:註釋處理器'android.databinding.annotationprocessor.ProcessDataBinding'找不到 1錯誤 – kevingiroux