简体   繁体   中英

Android google analytics easytracker - track event call in onCreate method of launcher activity crashes

I'm using EasyTracker google analytics library and my app have multiple activities. I want to track as event device model and system version info each time on app launch.

This code in onCreate method of my launcher activity

EasyTracker.getTracker().trackEvent("Others", "App launch", "The os version", 0);

crashes with

06-15 21:24:30.898: WARN/dalvikvm(1203): threadid=8: thread exiting with uncaught exception (group=0x4001d800)
06-15 21:24:30.898: ERROR/AndroidRuntime(1203): FATAL EXCEPTION: TrackerThread
        java.lang.NullPointerException
        at com.google.android.apps.analytics.GoogleAnalyticsTracker.createEvent(Unknown Source)
        at com.google.android.apps.analytics.GoogleAnalyticsTracker.trackEvent(Unknown Source)
        at com.google.android.apps.analytics.easytracking.GoogleAnalyticsTrackerDelegateImpl.trackEvent(Unknown Source)
        at com.google.android.apps.analytics.easytracking.EasyTracker$11.run(Unknown Source)
        at com.google.android.apps.analytics.easytracking.EasyTracker$TrackerThread.run(Unknown Source)
06-15 21:24:30.918: WARN/ActivityManager(58): Force finishing activity com.mycompany.myapp/com.mycompany.activity.HomeActivity

What I'm doing wrong?

You get a NullPointerException because the tracker is initialized in the onStart method of the base class TrackedActivity, which happens after onCreate. However, TrackedActivity automatically tracks the creation of the activity (trackPageView) so normally you do not need trackEvent here. You can successfully call trackEvent on any other method between onStart and onStop on the activity's lifecycle.

Remember add the following code in activity.onCreate:

EasyTracker.getInstance().setContext(this);

before using any tracker feature.

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