我正在从活动中创建Google Analytics跟踪器对象。问题是我在this.getApplication()上执行的强制转换操作中出错。请告诉我如何从活动中创建跟踪器对象。
public void sengToGoogleAnalytics() {
Tracker t = ((AnalyticsSampleApp) this.getApplication()).getTracker(TrackerName.APP_TRACKER);
t.setScreenName(getString(R.string.memoryhome));
t.send(new HitBuilders.AppViewBuilder().build());
}
日志
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):
答案 0 :(得分:18)
<强>原因:强>
getApplication返回的对象为android.app.Application
,抛出ClassCastException,如logcat中的以下行所示
06-11 11:49:59.510:E / AndroidRuntime(2917):引起:java.lang.ClassCastException:android.app.Application无法强制转换为kids.animals.fruits.objects.brain.puzzle.memory .game.free.AnalyticsSampleApp06-11
<强>解决方案:强>
如果您的清单不包含application element
<application android:name=".YourApplication"...
</application>