2

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>
StingRay5
  • 2,388
  • 2
  • 20
  • 29
  • *Nexus 6 running API 23*, do you mean the preview image of Android M ? – Blackbelt Sep 07 '15 at 10:28
  • Yes. Build number MPA44I. – StingRay5 Sep 07 '15 at 10:32
  • the same pattern is working fine on my device. Any stacktrace you want to share ? – Blackbelt Sep 07 '15 at 10:33
  • 1
    U should avoid to do such thing, u are leaking your application by keeping a static! just cast the application context as following: myapp = (MyApp)getApplicationContext(); – Anis LOUNIS aka AnixPasBesoin Sep 07 '15 at 10:33
  • @Blackbelt, strange stuff, I don't have not much to share, except a java.lang.NullPointerException that attempts to invoke virtual method on a null object reference. – StingRay5 Sep 07 '15 at 10:56
  • post that one. Let's have a look\ – Blackbelt Sep 07 '15 at 10:57
  • @AnixPasBesoin, I've read the several threads and questions about this topic, and made my choices, since it's working perfectly for all other cases, but it's still strange that onCreate was never called in this particular one.... – StingRay5 Sep 07 '15 at 10:59
  • Can u post your manifest file? have u tried moving "APP_INSTANCE = this;" before "super.onCreate();" – Anis LOUNIS aka AnixPasBesoin Sep 07 '15 at 11:22
  • There is a clear race condition between the `getInstance` and the `onCreate` method. If `super.onCreate()` doesn't finish fast and the system doesn't wait for `onCreate` before `getInstance` gets called you get `null`, the default assigned value. The major question is: why do you need `getInstance` at all? – Maarten Bodewes Sep 07 '15 at 11:28
  • @MaartenBodewes I've noticed that, but even when I exclude all calls, getting the instance later also results in a null exception, it seems like onCreate never gets called after intial installation. In my Application class I check whether the app was updated or if it was first boot, to clear preferences. And although I might need to change it, I also keep a reference to a UserProfile to have a global way of checking whether my user is logged in. – StingRay5 Sep 07 '15 at 11:37
  • @AnixPasBesoin, yeah, tried putting the `APP_INSTANCE = this` before the `super.onCreate()` to no avail unfortunately. – StingRay5 Sep 07 '15 at 11:46
  • I'm no Android export, but I think you're abusing the `getInstance` method and state system of Android. These things you mention are either hidden explicitly from you or you must be able to check them without `getInstance`. In your application itself you have `this`, right? – Maarten Bodewes Sep 07 '15 at 11:58
  • 1
    Found part of the issue here: http://stackoverflow.com/a/8968915/718504 In the Application class, the onCreate() method is called only if the process was ended when you exited the application. So, if this is the case, it's perfectly clear why the application instance is NULL; it's never set. – StingRay5 Sep 07 '15 at 12:00
  • Ok... that shared comment, shouldn't make any sense, but it seems the case... http://developer.android.com/reference/android/app/Application.html#onCreate() – StingRay5 Sep 07 '15 at 12:08
  • Okay, there seems to be something fishy going on with `allowBackup` in the Manifest: https://code.google.com/p/android-developer-preview/issues/detail?id=2965 Changing the the parameter to false solved the issue instantly. – StingRay5 Sep 07 '15 at 12:16
  • I am very late on this issue, but I think this occurs when app data are restored from the cloud : https://stackoverflow.com/a/60597718/1333448 – Yves Delerm Mar 09 '20 at 09:33

0 Answers0