简体   繁体   中英

google analytics- classcasexception

i'm new to android and try to implement google analytic in to my project where its shows classcastexception in below set of code:

 Tracker t = ((AnalyticsHelper) MainActivity.this.getApplication()).getTracker(
            TrackerName.APP_TRACKER);

And this is my AnalyticsHelper class:

public class AnalyticsHelper extends Application {


    // The following line should be changed to include the correct property id.
    private static final String PROPERTY_ID = "UA-xxxxxxxx-x"; // My Property id.

    public static int GENERAL_TRACKER = 0;

    public enum TrackerName {
        APP_TRACKER, // Tracker used only in this app.
        GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
        ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.
    }

    HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

    public AnalyticsHelper()
    {
        super();
    }

    synchronized Tracker getTracker(TrackerName trackerId) {
        if (!mTrackers.containsKey(trackerId)) {

            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            analytics.getLogger().setLogLevel(LogLevel.VERBOSE);
            Tracker t = null;
            if(trackerId==TrackerName.APP_TRACKER){
                t= analytics.getTracker(PROPERTY_ID);
            }
                 mTrackers.put(trackerId, t);
                }
        return mTrackers.get(trackerId);
    }

i have include both jar files under libs (Google play service and google analytics services) And also i have mention in manifest file like this:

<activity
            android:name="com.xxxx.xxxx.AnalyticsHelper"
            android:configChanges="screenSize"

            android:screenOrientation="sensorPortrait" >
        </activity>

log_cat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.xxxx/com.xxxx.xxxx.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.xxxx.xxxx.AnalyticsHelper

what went wrong did i missing something ..please help thanks in advance

Remove ( AnalyticsHelper is not activity but a custom application class)

<activity
    android:name="com.xxxx.xxxx.AnalyticsHelper"
    android:configChanges="screenSize"
    android:screenOrientation="sensorPortrait" >
</activity>

and add the context to application tag like this

<application
    android:name="com.xxxx.xxxx.AnalyticsHelper"
    android:icon="@drawable/icon"
    android:label="@string/app_name">

You can get the Tracker instance by this only, no need to write that long code

Tracker t = ((AnalyticsHelper)getApplicationContext()).getTracker(TrackerName.APP_TRACKER);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM