We're getting an IllegalStageException when trying to initialize our shared preferences singleton class but I don't know what's causing it.
Can anyone here advise me as to what may be causing this?
NOTE: This is ONLY happening on Pie
Here's the report from the Google Play Console (crash is not showing in Crashlytics):
java.lang.RuntimeException:
at android.app.ActivityThread.handleBindApplication (ActivityThread.java:5876)
at android.app.ActivityThread.access$1100 (ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1650)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:193)
at android.app.ActivityThread.main (ActivityThread.java:6669)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:858)
Caused by: java.lang.IllegalStateException:
at android.app.ContextImpl.getSharedPreferences (ContextImpl.java:419)
at android.app.ContextImpl.getSharedPreferences (ContextImpl.java:404)
at android.content.ContextWrapper.getSharedPreferences (ContextWrapper.java:174)
at com.nbc.news.utils.SharedPreferences.<init> (SharedPreferences.java:27)
at com.nbc.news.utils.SharedPreferences.init (SharedPreferences.java:44)
at com.nbc.news.NbcNews.onCreate (NbcNews.java:122)
at android.app.Instrumentation.callApplicationOnCreate (Instrumentation.java:1154)
at android.app.ActivityThread.handleBindApplication (ActivityThread.java:5871)
Here's the init code:
private android.content.SharedPreferences sharedPreferences;
private static SharedPreferences instance;
private SharedPreferences(Context context) {
Context appContext = context.getApplicationContext();
sharedPreferences = appContext.getSharedPreferences(context.getString(R.string.database_name), Context.MODE_PRIVATE); // <- this is line #27
}
public android.content.SharedPreferences getPreference(){
return sharedPreferences;
}
public static SharedPreferences getInstance() {
if (instance == null) {
throw new RuntimeException("SharedPreferences must be initialized with context prior to use");
}
return instance;
}
public static void init(Context context) {
if (instance == null) {
instance = new SharedPreferences(context); // <- this is line #44
}
}