Dr. KoK
Dr. KoK

Reputation: 13

How can i fix AnalyticsTrackers error in Android

I want use Analytics in my application and i write below codes, but when user click on logout and logout from my application, show me force close error !

AnalyticsTrackers codes:

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

MyApplication codes:

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

Error in 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) 

How can i fix this problem? thanks all <3

Upvotes: 0

Views: 399

Answers (1)

Janbazam
Janbazam

Reputation: 11

I was also facing the same issue caused by IllegalStateException at AnalyticsTracker.java. I was able to solve the issue by a little modification in the AnalyticsTracker.java. Here is the modification.

Replace the code of the initiliaze() method

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

        sInstance = new AnalyticsTrackers(context);

with

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

With this modification, you will stop throwing exception and prevent repetitive instances initializations.

Upvotes: 1

Related Questions