我的应用程序在该方法中进行了大量初始化Application::onCreate()
,并且我有一个检查,如果它被调用两次,它将引发异常。
但是,在某些特定情况下,Application::onCreate()
会调用两次。我在中添加了以下Log
语句onCreate()
:(instance
是一个静态变量)
Log.i(TAG, "Application::ctor, this=" + this + " instance=" + instance + " pid="
+ android.os.Process.myPid());
我有两个日志(通常,instance == null
):
09-20 19:14:42.151 {main} [INFO] [Application] Application::ctor, this=com.shawnma.MyApplication@428886b0 instance=null pid=10741
09-20 19:14:50.116 {main} [INFO] [Application] Application::ctor, this=com.shawnma.MyApplication@42923e00 instance=com.shawnma.MyApplication@428886b0 pid=10741
在这种情况下,我从该onCreate()
方法返回 - 它不会受到伤害,因为所有内容都应该始终在内存中。但是,在 Android 系统之后立即显示另一个异常:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.innopath.mobilemd/com.innopath.mobilemd.ui.LauncherActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:100)
at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:51)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2019)
... 11 more
似乎第二次创作是由唤醒Application
触发的。ALARM_MANAGER
但是,应用程序的第一次创建应该已经完成并保存在内存位置?