Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 460
M.Y. Avatar asked Jun 15 '12 16:06

M.Y.


1 Answers

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.

like image 190
Victor G Avatar answered Sep 22 '22 20:09

Victor G