Home>

Android kotlin beginner with experience in other languages.
Because it crashes at the time of starting with custom with realmrecyclerviewadapter, when debugging it
realm = Realm.getDefaultInstance<-This will be null.
(When I tried the sample code in the primer, it was displayed as Realm @ number.)
version matches the version of the primer. (I tried the latest version, but it was useless.)
A manifest is also registered.<application android: name =".HabitApplication".....
This is a rudimentary question, but couldn't you give me advice because I couldn't find what was wrong?

Applicable source code
buildscript {
    ext.kotlin_version = '1.2.30'
    ext.anko_version = "0.10.4"
    repositories {
        google ()
        jcenter ()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.1'
        classpath "org.jetbrains.kotlin: kotlin-gradle-plugin: $kotlin_version"
        classpath "io.realm: realm-gradle-plugin: 5.0.1"
        // NOTE: Do not place your application dependencies here;they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        google ()
        jcenter ()
    }
}
task clean (type: Delete) {
    delete rootProject.buildDir
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'
apply plugin: 'kotlin-kapt'
android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.pegas.realmapplication"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile ('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    implementation 'io.realm: android-adapters: 2.1.1'
    implementation "org.jetbrains.anko: anko-common: $anko_version"
    implementation fileTree (dir: 'libs', include: ['* .jar'])
    implementation "org.jetbrains.kotlin: kotlin-stdlib-jre7: $kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint: constraint-layout: 1.1.2'
    testImplementation 'junit: junit: 4.12'
    androidTestImplementation 'com.android.support.test: runner: 1.0.2'
    androidTestImplementation 'com.android.support.test.espresso: espresso-core: 3.0.2'
}
package com.example.pegas.realmapplication
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import io.realm.Realm
class MainActivity: AppCompatActivity () {
    private lateinit var realm: Realm
    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)
        realm = Realm.getDefaultInstance () //&lt;--- null !!!!!!!!!!!!
        Log.d ("TAG", "oooooooooooo")
    }

    override fun onDestroy () {
        super.onDestroy ()
        realm.close ()
    }
}
package com.example.pegas.realmapplication
import android.app.Application
import io.realm.Realm
class HabitApplication: Application () {
    override fun onCreate () {
        super.onCreate ()
        Realm.init (this)
    }
}
package com.example.pegas.realmapplication
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import java.util. *
open class Habit: RealmObject () {
    @PrimaryKey
    var id: Long = 0
    var date: Date = Date ()
    var detail: String = ""
    var title: String = ""
}
  • Answer # 1

    It was obtained by executing from java realm official reference. I didn't know the cause, but I think again when learning progresses.