Android应用程序实例被清除为null [英] Android application instance is cleared as null

查看:94
本文介绍了Android应用程序实例被清除为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Application实例另存为我的应用程序中的静态成员,如下所示:

I saved the Application instance as the static member in my Application, like below:

public class MyApplication extends MultiDexApplication {
    private static MyApplication sInstance;

    public void onCreate() {
        super.onCreate();
        sInstance = this;
        // other code
    }

    public static MyApplication get(){
        return sInstance;
    }
}

MyApplication中没有类似于set的方法.因此,按照我的假设,在我的应用的整个生命周期中引用的sInstance都不应为null.

There is no set-like method in the MyApplication. So in my assumption, the sInstance should never be null where refered in the entire lifecycle of my app.

但是实际上,我发现由MyApplication.get()返回null导致的一些NPE崩溃,即使它在Activity onCreate/onDestroy中被调用也是如此.

But in fact, I found a few NPE crashes caused by MyApplication.get() returning null, even it's called in the Activity onCreate/onDestroy.

我只是不明白为什么sInstance为null.我认为,在启动该过程之前,应先将应用程序初始化.有人知道为什么吗?

I just don't understand why the sInstance is null. In my opinion, the application should be initilized before any other Activities when the process is launched. Does anyone know why?

推荐答案

我的应用程序日志中发生了类似的崩溃,MyApplication.get()方法上的NPE崩溃了.经过长时间的搜索,我发现此崩溃是由从云中还原应用程序引起的.

I have had a similar crash in my application logs, NPE crash on MyApplication.get() method. After long search, I discovered that this crash was caused by the app restoration from the cloud.

如果清单文件的应用程序标记为android:allowBackup="true",也可能是您的情况.

If you have android:allowBackup="true" if the application tag of your manifest file, it could be your case too.

还原应用程序数据后,该应用程序将以不寻常的方式被杀死,并且当用户再次启动该应用程序时,将永远不会调用MyApplication.onCreate方法.

When the app data is restored, the app is killed in an unusual way, and when the user starts the app again, MyApplication.onCreate method is never called.

重现此崩溃的简便步骤是:

Easy steps to reproduce this crash are :

1)在设备设置中启用将应用程序数据保存在云上.

1) enable the app data saving on cloud in the device settings.

2)通过启动adb shell bmgr backupnow my.package.name

3)启动应用

4)从命令行触发云备份:adb shell bmgr restore my.package.name =>应用程序将停止

4) trigger cloud backup from the command line : adb shell bmgr restore my.package.name => the app will stop

5)通过单击其图标=>再次启动该应用,该应用因NPE而崩溃

5) launch the app again by clicking on its icon => the app crashes with a NPE

您可以通过重复步骤4和5再次触发崩溃.

You can trigger the crash again by repeating steps 4 and 5.

有多种方法可以解决此崩溃问题:

There are differents ways to solve this crash :

  • 禁用备份:android:allowBackup="false"

从活动getApplication方法获取一个应用程序实例(警告:我不确定它将是MyApplication对象)

get an app instance from the activity getApplication method (caution : I am not sure it will be a MyApplication object)

在应用程序实例为空时强制杀死并重新启动应用程序

force killing and restarting the app when application instance is null

使用自定义的BackupAgent避免该问题

use a custom BackupAgent to avoid the issue

这篇关于Android应用程序实例被清除为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆