On Google Play I receive NullPointerException
, on trying to access static Application instance. It appears on different Android OS versions: Android 9/8/6
From code it seems, Application.onCreate()
hasn't been called / called after Activity.onCreate()
.
I saw similar questions but without any useful response: https://stackoverflow.com/questions/53607129/application-oncreate-callednot-called-after-activity-oncreate
As side solution, I'm going to create separate Singleton class depending on context, and get it with it (Context), but not sure, how it will work with Dagger in future.
My question is: 1. Is it Android bug? 2. Am I doing something wrong? 3. How other developers solve it?
public class App extends androidx.multidex.MultiDexApplication implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 {
private static App instance;
public static App getInstance() {
return instance;
}
@Override
public void onCreate() {
super.onCreate();
instance = this;
// init singletone
}
private Singletone singletone;
public Singletone getSingletone() {}
}
public class MainActivity extends androidx.appcompat.app.AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// On Google Play it produces NullPointerException
Singletone singletone = App.getInstance().getSingletone();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package.name">
<application
android:name=".app.App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar"
android:largeHeap="true">
<activity
android:name=".activities.MainActivity"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustPan"
android:theme="@style/AppTheme.NoActionBar.Launcher">
</activity>
</application>
</manifest>