OneSignal/OneSignal-Android-SDK

[Bug]: java.lang.Exception: Must call 'initWithContext' before use on Latest Android SDK

devnayan94 opened this issue · 3 comments

What happened?

I tested the SDK on my few devices no crashes but when deployed the new update on play store, I am getting alot of crashes.

This is happening for many users and is happening within 3 seconds.

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bigcash.app/com.bigcash.app.MainActivity}: java.lang.Exception: Must call 'initWithContext' before use
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3751)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3919)
       at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2311)
       at android.os.Handler.dispatchMessage(Handler.java:111)
       at android.os.Looper.loopOnce(Looper.java:238)
       at android.os.Looper.loop(Looper.java:357)
       at android.app.ActivityThread.main(ActivityThread.java:8098)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1026)

Steps to reproduce?

Not able to replicate on my end.

What did you expect to happen?

Dont crash?

OneSignal Android SDK version

Release 5.1.6

Android version

13, 12, 11, 10

Specific Android models

No response

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

@devnayan94 Thanks for reporting. Based on the error and behavior your are describing it seems your code may not always be calling OneSignal.initWithContext first.

Could you show and explain where your are calling OneSignal.initWithContext? It is highly recommended to call OneSignal.initWithContext from your Application.onCreate so it is always called first.

Hello!
Thank you for replying. Yes, I am calling it first and then calling other functions. For now, I am just checking if the SDK is initialised and then only calling other functions. For now issue is fixed on android 8, 9 ,10 and further versions.

But I am getting this error for android 6.0.1 and below:

Fatal Exception: java.lang.NoClassDefFoundError: com.bigcash.app.-$$Lambda$MainActivity$hn6a_fR8xqs3I7LpCavdIpl7I34
at com.bigcash.app.MainActivity.onCreate(MainActivity.java:234)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
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)

The class it can't find is com.bigcash.app.-$$Lambda$MainActivity$hn6a_fR8xqs3I7LpCavdIpl7I34 in your error message. This isn't a OneSignal class so I don't believe there is much we can do to help here.

That said, the issue is most likely your Android project isn't configured correctly. See this post: https://stackoverflow.com/a/31898800/1244574