无法使用Kotlin

时间:2017-01-15 15:16:18

标签: java android realm kotlin

我正在尝试使用Realm在Kotlin中构建一个测试项目。 这是我的模特:

open class Book : RealmObject() {

    // Standard getters & setters generated by your IDE…
    @PrimaryKey
    open var id: Long = 0

    open var title = ""

    open var description = ""

    open var author = ""

    open var imageUrl = ""
}

这是我得到的例外:

FATAL EXCEPTION: main
                                                                        Process: app.androidhive.info.realm, PID: 18782
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{app.androidhive.info.realm/app.androidhive.info.realm.activity.MainActivity}: java.lang.IllegalArgumentException: Book is not part of the schema for this Realm
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                         Caused by: java.lang.IllegalArgumentException: Book is not part of the schema for this Realm

我搜索了解决方案,并在build.gradle中添加了classpath "io.realm:realm-gradle-plugin:2.2.2" apply plugin: 'realm-android'

dependencies {
    ...
    // Realm
    compile 'io.realm:realm-android:0.87.4'
    kapt "io.realm:realm-annotations:0.87.4"
    kapt "io.realm:realm-annotations-processor:0.87.4"
}

进入模块应用程序的构建脚本。它给了我另一个问题:

Error:(15, 48) Cannot access '<init>': it is public/*package*/ in 

'Builder'
/Volumes/Toshiba/Users/macuser/Development/Android/Exersises/MyApplication/app/src/main/java/realmtest/realm/RealmController.kt
Error:(27, 15) Unresolved reference: refresh
Error:(34, 15) Unresolved reference: clear
Error:(53, 23) Unresolved reference: allObjects

[KOTLIN] deleting /Volumes/Toshiba/Users/macuser/Development/Android/Exersises/MyApplication/app/build/tmp/kotlin-classes/debug on error

如果Book是用java编写的,我只能成功构建我的项目。 有任何建议如何让Realm和Kotlin一起工作吗?

2 个答案:

答案 0 :(得分:2)

使用RealmClass注释+ RealmModel接口

import io.realm.Realm
import io.realm.RealmModel
import io.realm.annotations.PrimaryKey
import io.realm.annotations.RealmClass

@RealmClass
open class Example(
    @PrimaryKey open var Id : Long = 0,
    open var Field : String = ""
) : RealmModel

在模块gradle文件中添加

apply plugin: 'realm-android'

在项目gradle文件中添加

classpath "io.realm:realm-gradle-plugin:2.2.2"

不需要额外的编译/ kapt

答案 1 :(得分:2)

Realm java 4.1.0已经发布,而且Kotlin的Realm大多数问题已经解决了!您无需使用open var即可使用var。您可以测试my sample project。我的示例是用于使用kotlin在Realm对象服务器中测试模块。