2016-03-07 35 views
-1

科特林版本1.0.0,版本境界0.88.0-快照 我下載的境界科特林演示,並運行 如果這樣的代碼:科特林境界演示canot運行

var person = Person() 
    person.id = 1 
    person.name = "Young Person" 
    person.age = 14 
    realm.beginTransaction() 
    realm.copyToRealm(person) 
    realm.commitTransaction() 

拋出異常:產生的原因: java.lang.ClassCastException:io.realm.examples.kotlin.model.Person不能轉換到io.realm.PersonRealmProxyInterface

否則我改變這樣的代碼:

realm.beginTransaction() 
    // Add a person 
    var person = realm.createObject(Person::class.java) 
    person.id = 1 
    person.name = "Young Person" 
    person.age = 14 
    // When the transaction is committed, all changes a synced to disk. 
    realm.commitTransaction() 

然後領域插入一個數據,但是person.name,id,age是空值或0;

如何解決呢

+0

克隆'realm-java'項目後,先嚐試在項目的根目錄下運行'/ gradlew installRealmJava'。然後建立這個例子。 – beeender

+0

你不顯示你的Person類,很高興看到。 –

回答

3

隨着你必須用自己的搖籃插件以及境界0.88.0快照。如果谷歌這個異常(realm java.lang.ClassCastException ProxyInterface),你會發現這Github Issue - 2353它說:

We just merged our byte code weaver into master, and it sounds like it isn't being triggered in your case. Note that from 0.88.0-SNAPSHOT you have to use our Gradle plugin: https://realm.io/news/android-installation-change/

以前,您安裝領域,如:

repositories { 
    jcenter() 
} 

dependencies { 
    compile 'io.realm:realm-android:<version>' 
} 

現在你必須安裝它也包括了搖籃插件:

buildscript { 
    repositories { 
     jcenter() 
    } 
    dependencies { 
     classpath "io.realm:realm-gradle-plugin:<version>" 
    } 
} 

apply plugin: 'realm-android' 

使字節碼編織打開。如果沒有這個,你就會收到一個與你的第一個用例完全相同的錯誤(這是正確的代碼,但對於你選擇使用的快照版本,你還需要Gradle插件的額外步驟)。上面的鏈接中還有其他重要的註釋,以及Realm最新版本中的更改發行說明。

+0

我添加了'classpath'io.realm:realm-gradle-plugin:「'和'apply plugin:'realm-android'',但錯誤如'錯誤:Gradle:執行失敗,任務':app:transformClassesWithJarMergingForDebug'。 > com.android.build.api.transform.TransformException:java.util.zip.ZipException:重複條目:io/realm/annotations/Ignore.class'有時是Index.class,我發現:[link](https ://github.com/realm/realm-java/issues/1545 )和[鏈接](https://github.com/realm/realm-java/issues/1807),但canot解決它,幫助,thx – vslimit

+0

你能否開始一個新的問題,你需要包含你的依賴列表以及gradle dependencies命令的輸出,因爲它會顯示另外一個Realm庫的副本。這裏不能解決這個問題。 –