如何修复Android中的AnalyticsTrackers错误

时间:2016-12-13 10:30:41

标签: android

我想在我的应用程序中使用 Analytics ,我会在下面编写代码,但是当用户点击我的应用程序中的注销注销时,请显示我强行关闭错误!

AnalyticsTrackers代码:

public final class AnalyticsTrackers {

    private static AnalyticsTrackers sInstance;
    private final Context mContext;

    /**
     * Don't instantiate directly - use {@link #getInstance()} instead.
     */
    private AnalyticsTrackers(Context context) {
        mContext = context.getApplicationContext();
    }

    public static synchronized void initialize(Context context) {
        if (sInstance != null) {
            throw new IllegalStateException("Extra call to initialize analytics trackers");
        }

        sInstance = new AnalyticsTrackers(context);
    }

    public static synchronized AnalyticsTrackers getInstance() {
        if (sInstance == null) {
            throw new IllegalStateException("Call initialize() before newInstance()");
        }

        return sInstance;
    }


    public enum Target {
        APP,
        // Add more trackers here if you need, and update the code in #get(Target) below
    }
}

我的应用代码:

public class MyApplication extends Application {
    private static final String TAG = "MyApplication";
    // The following line should be changed to include the correct property id.
    private static Context mContext;

    public static Context getContext() {
        return mContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        MultiDex.install(this);
        mContext = getApplicationContext();
        if (Utils.checkPlayServices(mContext) && Utils.skipLogin(mContext)) {
            // Start IntentService to register this application with GCM.
            Intent intent = new Intent(this, RegistrationIntentService.class);
            startService(intent);
        }
        Fabric.with(this, new Crashlytics());
        AnalyticsTrackers.initialize(mContext);
        FileDownloader.init(mContext);
        }

    }

    @Override
    protected void attachBaseContext(Context base) {
        try {
            super.attachBaseContext(base);
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
//      MultiDex.install(this);
    }
}

logcat错误:

E/CrashlyticsCore: Tried to write a fatal exception while no session was open.
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: ir.mototel.mototel, PID: 7257
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.mototel.mototel/com.sepandar.xengine.activity.MainActivity}: java.lang.RuntimeException: Unable to create application com.android.tools.fd.runtime.BootstrapApplication: java.lang.IllegalStateException: Extra call to initialize analytics trackers
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   Caused by: java.lang.RuntimeException: Unable to create application com.android.tools.fd.runtime.BootstrapApplication: java.lang.IllegalStateException: Extra call to initialize analytics trackers
                      at android.app.LoadedApk.makeApplication(LoadedApk.java:591)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2334)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                   Caused by: java.lang.IllegalStateException: Extra call to initialize analytics trackers
                      at com.sepandar.xengine.util.AnalyticsTrackers.initialize(AnalyticsTrackers.java:34)
                      at com.sepandar.xengine.MyApplication.onCreate(MyApplication.java:43)
                      at com.android.tools.fd.runtime.BootstrapApplication.onCreate(BootstrapApplication.java:370)
                      at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1013)
                      at android.app.LoadedApk.makeApplication(LoadedApk.java:588)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2334) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我该如何解决这个问题?谢谢所有< 3

1 个答案:

答案 0 :(得分:1)

我还面临由AnalyticsTracker.java中的IllegalStateException引起的同一问题。我能够通过在AnalyticsTracker.java中进行一些修改来解决问题。这是修改。

替换initiliaze()方法的代码

if (sInstance != null) {
            throw new IllegalStateException("Extra call to initialize analytics trackers");
        }

        sInstance = new AnalyticsTrackers(context);

if (sInstance == null) {
            sInstance = new AnalyticsTrackers(context);
        }

通过此修改,您将停止抛出异常并防止重复实例初始化。