Google Analytics - Tracker Object from Activity

2019-04-01 01:12发布

问题:

I am creating the Google Analytics Tracker object from an Activity. The issue is that I am getting an error in the cast operation done on this.getApplication(). Please let me know how to create a tracker object from an Activity.

public void sengToGoogleAnalytics() {
    Tracker t = ((AnalyticsSampleApp) this.getApplication()).getTracker(TrackerName.APP_TRACKER);
    t.setScreenName(getString(R.string.memoryhome));
    t.send(new HitBuilders.AppViewBuilder().build());
} 

Log

06-11 11:49:59.510: E/AndroidRuntime(2917): FATAL EXCEPTION: main06-11 11:49:59.510: E/AndroidRuntime(2917): java.lang.RuntimeException: Unable to start activity ComponentInfo{kids.animals.fruits.objects.brain.puzzle.memory.game.free/kids.animals.fruits.objects.brain.puzzle.memory.game.free.MemoryHome}: java.lang.ClassCastException: android.app.Application cannot be cast to kids.animals.fruits.objects.brain.puzzle.memory.game.free.AnalyticsSampleApp06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): java.lang.RuntimeException: Unable to start activity ComponentInfo{kids.animals.fruits.objects.brain.puzzle.memory.game.free/kids.animals.fruits.objects.brain.puzzle.memory.game.free.MemoryHome}: java.lang.ClassCastException: android.app.Application cannot be cast to kids.animals.fruits.objects.brain.puzzle.memory.game.free.AnalyticsSampleApp06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to kids.animals.fruits.objects.brain.puzzle.memory.game.free.AnalyticsSampleApp
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to kids.animals.fruits.objects.brain.puzzle.memory.game.free.AnalyticsSampleApp06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to kids.animals.fruits.objects.brain.puzzle.memory.game.free.AnalyticsSampleApp06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 06-11 11:49:59.510: E/AndroidRuntime(2917): 
06-11 11:49:59.510: E/AndroidRuntime(2917): 

回答1:

Cause:
The object returned by getApplication is android.app.Application, which throws a ClassCastException, as indicated by the following line in the logcat

06-11 11:49:59.510: E/AndroidRuntime(2917): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to kids.animals.fruits.objects.brain.puzzle.memory.game.free.AnalyticsSampleApp06-11

Solution:
This can be caused if your manifest doesn't contain the application element

<application android:name=".YourApplication"...
</application>