I put Realm.init(this) in the onCreate of my Application class.
But it throws an exception while I'm calling Realm.getDefaultInstance() in the onCreate of my Activity.
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2511)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1360)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5480)
at java.lang.reflect.Method.invoke(Method.java)
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.IllegalStateException: Call `Realm.init(Context)` before calling this method.
at io.realm.Realm.getDefaultInstance(Realm.java:208)
at com.kimi.fastdb.PrefActivity.getRealmHelper(PrefActivity.java:1724)
at com.kimi.fastdb.PrefActivity.onCreate(PrefActivity.java:270)
at com.kimi.fastdb.LauncherActivity.onCreate(LauncherActivity.java:464)
at android.app.Activity.performCreate(Activity.java:6308)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
Why the realm instance not been initialized after my Application starts? And how to fix it?
Application:
public class App extends MultiDexApplication {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.schemaVersion( REALM_DATABASE_VERSION ) // Must be bumped when the schema changes
.migration( migration ) // Migration to run instead of throwing an exception
// .deleteRealmIfMigrationNeeded()
.build();
Realm.compactRealm( realmConfiguration );
Realm.setDefaultConfiguration(realmConfiguration);
}
...
}
AndroidManifest.xml
<application
android:name=".App"
android:allowBackup="true"
android:allowClearUserData="true"
android:allowTaskReparenting="false"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@style/SplashScreenTheme">
...
</application>
Try :
Realm.init(getApplicationContext());
Realm realm = Realm.getDefaultInstance();
If you have specific configuration :
Realm.init(getApplicationContext());
Realm realm = Realm.getInstance(RealmConfiguration yourConfg);
After that, you can begin transaction and commit it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With