Realm - Realm.init(this) failed in onCreate?

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

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> 

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!