Android应用程序类方法的onCreate被多次调用 [英] Android Application class method onCreate being called multiple times

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

问题描述

我已经超载的应用程序类在我的Andr​​oid应用程序,我使用的是 ACRA 报告制度。 我的应用程序看起来像(<一href="https://github.com/evilsocket/dsploit/blob/master/src/it/evilsocket/dsploit/DSploitApplication.java"相对=nofollow>真正来源$ C ​​$ C这里):

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();
    }
}

而据我所知,应用程序对象应创建只有一次,所以onCreate方法应该被调用一次。 问题是,在我的崩溃报告(来自ACRA)我有这样的:

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)

因此​​,它似乎是应用程序的onCreate被多次调用,在此任何想法?

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

注:

  • 在我的Andr​​oid XML清单,我做的不可以使用 机器人:工艺=字符串属性
  • 我敢肯定,在我的初始化程序我是不可以小心 调用MyApplication.onCreate。
  • 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 .

推荐答案

如果你看一下堆栈跟踪,它看起来像 ACRA.init 呼吁 makeApplication 。我怀疑有某种code,以检查是否该应用程序已被创建,如果没有,创建它,它是由您的通话 ACRA.init super.onCreate 。一般覆盖的onCreate 方法时,(是否应用程序活动)它的建议调用 super.onCreate 作为实施的第一线,做您的自定义的东西之后。我给这一个镜头,看看它是否解决的事情。

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.

这篇关于Android应用程序类方法的onCreate被多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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