realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Gradle 2.2.3 crashing Caused by: java.lang.IllegalStateException: Call `Realm.init(Context)` before calling this method.

Open defaultbr opened this issue 9 years ago • 19 comments

after updating the Android Studio (Today-12/07/16) to the new Gradle 2.2.3 and doing a CLEAN, im getting this crash:

Caused by: java.lang.IllegalStateException: Call Realm.init(Context) before calling this method.

my application:

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        MultiDex.install(this);
        ACRA.init(this);
        Realm.init(this);
        RealmConfiguration config = new RealmConfiguration.Builder()
                .name("default.realm")
                .schemaVersion(Migration.VERSAO)
                .migration(new Migration())
                .build();
        Realm.setDefaultConfiguration(config);
        Log.d("Application", "onCreate -> Started");
    }

and the method that cause the crash: realm = Realm.getDefaultInstance();

Full Class

public class InitConfiguration extends AppCompatActivity {
...
...
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.atividade_init_configuration);
        realm = Realm.getDefaultInstance();
...
...
}

defaultbr avatar Dec 07 '16 03:12 defaultbr

Did you declare MyApp in your AndroidManifest.xml?

Have you tried disabling instant run?

Zhuinden avatar Dec 07 '16 06:12 Zhuinden

I couldn't reproduce this. I used https://github.com/zaki50/realm_template to reproduce.

zaki50 avatar Dec 07 '16 09:12 zaki50

@Zhuinden yes, i had. After deploying the application to a new Device(5.0) and returned to the 6.0 Device, it worked perfectly, dont know why

i will keep attention if it occur again

thanks guys

defaultbr avatar Dec 07 '16 14:12 defaultbr

I have an simillar issue.

Application class:

  @Override
    public void onCreate() {
        super.onCreate();
     
        // init realm db
        Realm.init(this);
}

First Activity:

protected void onCreate(Bundle savedInstanceState) {
            sRealmConfiguration = new RealmConfiguration.Builder()
                    .schemaVersion(4)
                    .migration(new CustomMigration())
                    .build();

            Realm.setDefaultConfiguration(sRealmConfiguration);
        }
  • Application class registered to manifest
  • classpath "io.realm:realm-gradle-plugin:2.0.2"
  • Gradle 2.2.2
  • Crash metrics based on about 20k User (from fabric):
    • Total affected user > 1%
    • 100% Android 6 (93% 6.0.1)
    • 67% Samsung

Crash:

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{...Activity}: java.lang.IllegalStateException: Call Realm.init(Context) before creating a RealmConfiguration at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) at android.app.ActivityThread.access$900(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:234) at android.app.ActivityThread.main(ActivityThread.java:5526) 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 creating a RealmConfiguration at io.realm.RealmConfiguration$Builder.(SourceFile) at io.realm.RealmConfiguration$Builder.(SourceFile) at ....initDatabase(SourceFile:109) at ....onCreate(SourceFile:57) at android.app.Activity.performCreate(Activity.java:6285) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) at android.app.ActivityThread.access$900(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:234) at android.app.ActivityThread.main(ActivityThread.java:5526) 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)

0xPixelfrost avatar Dec 12 '16 19:12 0xPixelfrost

I'm also experiencing problems. I see these crashes coming in.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.test/com.test.test.ui.activities.FirstActivity}: java.lang.IllegalStateException: Call `Realm.init(Context)` before calling this method.
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
	at android.app.ActivityThread.access$1100(ActivityThread.java:223)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:158)
	at android.app.ActivityThread.main(ActivityThread.java:7231)
	at java.lang.reflect.Method.invoke(Method.java)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.IllegalStateException: Call `Realm.init(Context)` before calling this method.
	at io.realm.Realm.getDefaultInstance(Realm.java:207)
	at com.test.test.ui.activities.FirstActivity.onCreate(FirstActivity.java:77)
	at android.app.Activity.performCreate(Activity.java:6877)
	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3209)
	... 9 more

Over 20 crashes in the last month.

Some devices on which this happens:

  • Samsung SM-J510FN
  • Sony E6653
  • Samsung SM-G900F
  • Samsung SM-N910C
  • Motorola Moto G (4)
  • Lenovo P2a42

These devices all have Android 6.0 or 6.0.1

I'm using Realm 2.3.0. The Application class is in my manifest. I'm calling Realm.init(context) and Realm.setDefaultConfiguration(config). For many of my users this just works. But some users experience this.

It looks like the Realm.init(context) doesn't get executed right in the Application class. What I'm going to do is in my first activity add a try catch for IllegalStateException and then do the Realm.init(context) again. This is far from ideal though.

Anybody knows what this might be?

kevinvanmierlo avatar Mar 09 '17 09:03 kevinvanmierlo

I'm also having the same problem which only affect Android 6. After some further search I think I might be related to this issue where Android 6 custom application's onCreate is not called for the first time. http://stackoverflow.com/questions/32066873/custom-application-not-created-on-android-m-final-preview

allentcm avatar Mar 15 '17 00:03 allentcm

What the hell android. We are getting quite a few crashes in our app, I'll try at least try/catch. Any ideas how to properly fix this? Maybe isInitialized helper method?

Possimpible avatar Apr 10 '17 11:04 Possimpible

@Possimpible If it is caused by the Android bug mentioned in https://github.com/realm/realm-java/issues/3879#issuecomment-286608788 , maybe you can call Realm.init() again in your first activity? It will do nothing if it is already initialized.

beeender avatar Apr 10 '17 11:04 beeender

Seems so, crashlytics reports only Marshmallow and up. So far I did the try/catch block, I'll push it to prod and see if it helps (which it should). Seemed slightly better than explicitly ask for init every time, but if you think asking for it is better I'll change that.

Possimpible avatar Apr 10 '17 11:04 Possimpible

I'm also having the same problem on Android 4.4, not only Android 6.0 ....

ghost avatar Apr 12 '17 04:04 ghost

Is there any solution?

giusecapo avatar May 18 '17 07:05 giusecapo

try/catch works in our prod

Possimpible avatar May 18 '17 08:05 Possimpible

Where did you put try/catch?

Thanks for your help!

giusecapo avatar May 19 '17 08:05 giusecapo

just simple

Realm realm;

try {
    realm = Realm.getDefaultInstance();
} catch (IllegalStateException fuckYouTooAndroid) {
    Realm.init(getApplicationContext());
    Realm.setDefaultConfiguration(/**/);
    realm = Realm.getDefaultInstance();
}

Possimpible avatar May 19 '17 13:05 Possimpible

Same situation here for Android 6 and up, but only in Broadcast receiver. Will try try/catch solution, let's see

denisvasyanin avatar Jun 19 '17 14:06 denisvasyanin

Does this solution works, can anyone confirm

try { realm = Realm.getDefaultInstance(); } catch (IllegalStateException fuckYouTooAndroid) { Realm.init(context.getApplicationContext()); realm = Realm.getDefaultInstance(); }

savvisingh avatar Jun 26 '17 11:06 savvisingh

This worked for me:

        Realm.init(this);
        RealmConfiguration config = new RealmConfiguration.Builder()
                .name(Realm.DEFAULT_REALM_NAME)
                .migration(new Migration())
                .deleteRealmIfMigrationNeeded()
                .schemaVersion(4)
                .build();
        Realm.setDefaultConfiguration(config);
        realm = Realm.getInstance(config);

soshial avatar Jan 07 '18 22:01 soshial

@savvisingh yes it work, thank you

mrBunhong avatar Sep 04 '18 03:09 mrBunhong

I also had that error reported many times on Google Play (only Android 6.0 with different devices) and can confirm that calling Realm.init() again on the MainActivity fixes that.

ClouddJR avatar Jan 04 '19 10:01 ClouddJR