0

Occasionally, from crash report, I can encounter Application instance is null, in my AppWidgetProvider.

This puzzles me quite some time, as I can never reproduce the same problem.

My setup is as following

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    ... >

    <application
        android:name=".JStockApplication"
        ... >

JStockApplication.java

public class JStockApplication extends android.support.multidex.MultiDexApplication  {
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    public void onCreate(){
        super.onCreate();

        me = this;

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        jStockOptions = JStockOptions.newInstance(sharedPreferences);
    }

    public static JStockApplication instance() {
        return me;
    }

    public JStockOptions getJStockOptions() {
        return jStockOptions;
    }
}

JStockAppWidgetProvider

public class JStockAppWidgetProvider extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // context is not null. But, I'm not sure what is context.
        //
        // From crash report, JStockApplication.instance() can be null rarely.
        // JStockApplication.instance().getJStockOptions() will cause NPE.
    }
}

Very rarely, JStockApplication.instance().getJStockOptions() will cause NPE!

I realize most ppl (https://stackoverflow.com/a/5114361/72437) perform

me = this;

in onCreate

I'm not sure, whether performing such initialization (and PreferenceManager.getDefaultSharedPreferences(this)) can help to resolve such mystery crash.

Do you have any idea, why NPE happen?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • Are `JStockAppWidgetProvider`, `JStockApplication`, and `JStockOptions` all in the same DEX file, since you are using mutlidex? – CommonsWare Jul 03 '17 at 14:11
  • I don't specific whether they should be the same dex or not. I just follow steps by steps instructions from https://developer.android.com/studio/build/multidex.html – Cheok Yan Cheng Jul 03 '17 at 14:53

1 Answers1

0

I know this question is a bit old but I just encountered a similar problem recently and it was caused by app data restoration from the cloud

Do you have android:allowBackup="true" in the application tag of your manifest ? If yes, this backup / restoration process could be involved.

When data is restored from the cloud, the app is shut down in a particular way, and when it is started again, Application.onCreate method is not called.

In order to reproduce that, here are some few steps :

  1. Enable app data backup on the cloud from the device settings
  2. Make sure that app data are saved on the cloud by forcing it on the command line : adb shell bmgr backupnow my.package.name
  3. Launch the app
  4. Trigger app date restoration from command line with : shell bmgr restore my.package.name ; the app will be stopped
  5. Just launch your app from its icon => it will crash with a NPE on Application.getInstance()

To reproduce this crash another time, just repeat steps 4 and 5.

Yves Delerm
  • 785
  • 4
  • 10