This seems a particular case, which for now only seems to occur on my Nexus 6 running API 23, but not on emulators or (a couple) 4.4 devices. I use my application class as a Singleton and usually it doesn't cause any problems. But somehow on the Nexus, it will crash the app on any reference to the application class, since it always returns null. After that, the app runs fine, so this only occurs after initial installation (when the app was deleted or not installed before).
public class MyApp extends Application {
private static MyApp APP_INSTANCE;
public static MyApp getInstance() { return APP_INSTANCE; }
@Override
public void onCreate() {
super.onCreate();
APP_INSTANCE = this;
// rest of code
}
}
So, whether I call MyApp.getInstance()
in my MainActivity
or even MainFragment
in onCreate
, onStart
or onResume
, it will always crash even after the app was already running.
When debugging the issue doesn't occur. It seems that the activity is being slowed down, and therefore has time to create the instance of the application class, however when I skip all initial calls to get the instance in the MainActivity
and then try to get it later (after log in) in my MainFragment
it still is null
.
Also I tried logging the whole process, and I see that the initialisation of the Application Class is done, since my HttpClients are setup and working, but the onCreate
doesn't get called.
So... anybody has an idea what could be causing this?
Added StackTrace of Initial Installation and Second Start
Initial Installation
09-07 12:46:43.240 2741-2741/com.myapp.d I/Process﹕ Sending signal. PID: 2741 SIG: 9
09-07 12:50:55.150 7068-7068/com.myapp.d W/System﹕ ClassLoader referenced unknown path: /data/app/com.myapp.d-1/lib/arm
09-07 12:50:56.346 7068-7068/com.myapp.d W/ClientFactory﹕ Returning UNSAFE client
09-07 12:50:56.360 7068-7068/com.myapp.d I/ClientFactory﹕ Supported CipherSuites: [...ciphersuits here...]
09-07 12:50:56.362 7068-7068/com.myapp.d I/MyApp﹕ Returning App Instance
09-07 12:50:56.362 7068-7068/com.myapp.d I/MainActivity﹕ Init UI
09-07 12:50:56.389 7068-7068/com.myapp.d I/AppCompatViewInflater﹕ app:theme is now deprecated. Please move to using android:theme instead.
09-07 12:50:56.446 7068-7068/com.myapp.d I/MyApp﹕ Returning App Instance
09-07 12:50:56.512 7068-7068/com.myapp.d I/MainActivity﹕ Check Startup
09-07 12:50:56.512 7068-7068/com.myapp.d E/MainActivity﹕ initApp(): start
09-07 12:50:56.512 7068-7068/com.myapp.d D/AndroidRuntime﹕ Shutting down VM
09-07 12:50:56.513 7068-7068/com.myapp.d E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myapp.d, PID: 7068
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.d/com.myapp.ui.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.myapp.MyApp.isFirstBoot()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
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.NullPointerException: Attempt to invoke virtual method 'boolean com.myapp.MyApp.isFirstBoot()' on a null object reference
at com.myapp.ui.MainActivity.initApp(MainActivity.java:173)
at com.myapp.ui.MainActivity.onCreate(MainActivity.java:116)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Second Boot
09-07 12:54:07.970 7068-7068/com.myapp.d I/Process﹕ Sending signal. PID: 7068 SIG: 9
09-07 12:54:10.386 8420-8420/com.myapp.d W/System﹕ ClassLoader referenced unknown path: /data/app/com.myapp.d-1/lib/arm
09-07 12:54:10.397 8420-8420/com.myapp.d W/ClientFactory﹕ Returning UNSAFE client
09-07 12:54:10.409 8420-8420/com.myapp.d I/ClientFactory﹕ Supported CipherSuites: [...ciphersuites here...]
09-07 12:54:10.410 8420-8420/com.myapp.d I/MyApp﹕ onCreate()
09-07 12:54:10.766 8420-8420/com.myapp.d I/CrashlyticsCore﹕ Initializing Crashlytics 2.3.4.74
09-07 12:54:10.811 8420-8420/com.myapp.d I/MyApp﹕ Returning App Instance
09-07 12:54:10.811 8420-8420/com.myapp.d I/MainActivity﹕ Init UI
09-07 12:54:10.833 8420-8420/com.myapp.d I/AppCompatViewInflater﹕ app:theme is now deprecated. Please move to using android:theme instead.
09-07 12:54:10.883 8420-8426/com.myapp.d W/art﹕ Suspending all threads took: 5.763ms
09-07 12:54:10.887 8420-8420/com.myapp.d I/MyApp﹕ Returning App Instance
09-07 12:54:10.953 8420-8420/com.myapp.d I/MainActivity﹕ Check Startup
09-07 12:54:10.953 8420-8420/com.myapp.d E/MainActivity﹕ initApp(): start
09-07 12:54:10.954 8420-8420/com.myapp.d I/MyApp﹕ Check for first boot: false
09-07 12:54:10.954 8420-8420/com.myapp.d I/MyApp﹕ Check for updated app: false
09-07 12:54:10.959 8420-8420/com.myapp.d D//SpiceManager.java:489﹕ 12:54:10.959 main adding request to request queue
09-07 12:54:10.959 8420-8420/com.myapp.d E/MainActivity﹕ initApp(): autoLogin
09-07 12:54:10.960 8420-8420/com.myapp.d E/MainActivity﹕ initApp(): end
And the Manifest file, nothing special, but just in case
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp">
<!-- all my permissions: nothing special -->
<application
android:name="com.myapp.MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app.label"
android:theme="@style/AppTheme">
<activity
android:name=".ui.MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Other activities and services below -->
<!-- And API KEYS go here -->
</application>
</manifest>