Android Application class method onCreate being called multiple times

独自空忆成欢 提交于 2019-12-17 15:51:38

问题


i've overloaded the Application class in my android app and i'm using the ACRA report system. My app looks like ( real source code here ) :

public class MyApplication extends Application 
{
    @Override
    public void onCreate() {        
        ACRA.init( this );

        /*
         * Initialize my singletons etc
         * ...
         * ...
         */
        super.onCreate();
    }
}

And as far as i know, the Application object should be created only once, so the onCreate method should be called only once. The problem is, that in my crash reports ( from ACRA ) i have this:

java.lang.RuntimeException: Unable to create service it.evilsocket.myapp.net.N ...
java.lang.RuntimeException: Unable to create service it.evilsocket.myapp.net.NetworkMonitorService: java.lang.RuntimeException: Unable to create application it.evilsocket.myapp.MyApplication: **java.lang.IllegalStateException: ACRA#init called more than once**
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2283)
    at android.app.ActivityThread.access$1600(ActivityThread.java:127)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4441)
    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:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to create application it.evilsocket.myapp.MyApplication: java.lang.IllegalStateException: ACRA#init called more than once
    at android.app.LoadedApk.makeApplication(LoadedApk.java:495)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2269)
    ... 10 more
Caused by: java.lang.IllegalStateException: ACRA#init called more than once
    at org.acra.ACRA.init(ACRA.java:118)
    at it.evilsocket.myapp.MyApplication.onCreate(MyApplication.java:46)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
    at android.app.LoadedApk.makeApplication(LoadedApk.java:492)
    ... 11 more
java.lang.RuntimeException: Unable to create application it.evilsocket.myapp.MyApplication: java.lang.IllegalStateException: ACRA#init called more than once
    at android.app.LoadedApk.makeApplication(LoadedApk.java:495)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2269)
    at android.app.ActivityThread.access$1600(ActivityThread.java:127)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4441)
    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:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: ACRA#init called more than once
    at org.acra.ACRA.init(ACRA.java:118)
    at it.evilsocket.myapp.MyApplication.onCreate(MyApplication.java:46)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
    at android.app.LoadedApk.makeApplication(LoadedApk.java:492)
    ... 11 more
java.lang.IllegalStateException: ACRA#init called more than once
    at org.acra.ACRA.init(ACRA.java:118)
    at it.evilsocket.myapp.MyApplication.onCreate(MyApplication.java:46)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
    at android.app.LoadedApk.makeApplication(LoadedApk.java:492)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2269)
    at android.app.ActivityThread.access$1600(ActivityThread.java:127)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4441)
    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:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)

So it seems like the app onCreate is being called multiple times, any idea on this ?

NOTES:

  • In my android xml manifest, i did NOT use the android:process="string" attribute.
  • Yes, i'm sure that in my initialization routines i'm not accidentally calling MyApplication.onCreate .

回答1:


If you look at the stack trace, it looks like ACRA.init is calling makeApplication. I suspect that there's some sort of code to check if the application has been created already and if not, create it and that it is caused by your calling ACRA.init before super.onCreate. Generally when overriding onCreate methods (whether Application or Activity) it's recommended to call super.onCreate as the first line of your implementation and do your custom stuff afterwards. I'd give that a shot and see if it fixes things.




回答2:


I think that you have additional process in your application. That is why Application.onCreate is called more than once. Look into your manifest file and try to find the activity or service with something like android:process= . This means that activity/service is starting in second Dalvik VM, and that's why another application instance is created.




回答3:


I'm also seeing this with ACRA 4.4.0 in the wild.

Perhaps something as simple as this under the init method?

if (mApplication != null) {
    throw new IllegalStateException("ACRA#init called more than once");
    //(return or finish or gracefully exit somehow)       
} else {
    mApplication = app;
    //and then continue with rest of acra init...

Edit: 12/27/12 As a follow up to this, it looks like Kevin has adopted these changes. Details are here: https://github.com/ACRA/acra/commit/cda06f5b803a09e9e7cc7dafae2c65c8fa69b861



来源:https://stackoverflow.com/questions/12809737/android-application-class-method-oncreate-being-called-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!