Android应用程序类方法onCreate被多次调用

时间:2012-10-09 23:10:53

标签: lifecycle acra android

我在我的Android应用程序中重载了Application类,我正在使用ACRA报告系统。 我的应用看起来像(real source code here):

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

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

据我所知,Application对象只应创建一次,因此onCreate方法只应调用一次。 问题是,在我的崩溃报告中(来自ACRA)我有这个:

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)

所以似乎onCreate的应用程序被多次调用,对此有任何想法吗?

备注:

  • 在我的android xml清单中,我做了 NOT 使用 android:process =“string”属性。
  • 是的,我确定在我的初始化例程中,我意外 调用MyApplication.onCreate。

3 个答案:

答案 0 :(得分:24)

我认为您的申请中还有其他流程。这就是为什么不止一次调用Application.onCreate的原因。查看您的清单文件,尝试使用 android:process = 之类的内容查找活动或服务。这意味着活动/服务在第二个Dalvik VM中启动,这就是创建另一个应用程序实例的原因。

答案 1 :(得分:7)

如果查看堆栈跟踪,看起来ACRA.init正在调用makeApplication。我怀疑有某种代码可以检查应用程序是否已经创建,如果没有,创建它并且它是由ACRA.init之前的super.onCreate调用引起的。通常,当覆盖onCreate方法(ApplicationActivity)时,建议您将super.onCreate作为实施的第一行,并在之后执行自定义内容。我会试一试,看看它是否能解决问题。

答案 2 :(得分:6)

我也在野外看到ACRA 4.4.0。

也许在init方法下这么简单?

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...

编辑:12/27/12作为对此的跟进,看起来Kevin已经采纳了这些变化。详细信息如下:https://github.com/ACRA/acra/commit/cda06f5b803a09e9e7cc7dafae2c65c8fa69b861