Android - Google 分析崩溃
Android - Google Analytics Crashed
我在 eclipse 上有一个项目,现在我尝试添加这个项目 Google Analytics,但是当我添加那个代码时,我的 MainActivity 应用程序崩溃了。
Tracker t = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
t.setScreenName("HomePage");
t.send(new HitBuilders.AppViewBuilder().build());
LogCat
07-10 00:59:35.352: E/AndroidRuntime(9968): FATAL EXCEPTION: main
07-10 00:59:35.352: E/AndroidRuntime(9968): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.interrail.railrehber/com.interrail.railrehber.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.interrail.railrehber.AnalyticsSampleApp
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.ActivityThread.access0(ActivityThread.java:141)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.os.Looper.loop(Looper.java:137)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-10 00:59:35.352: E/AndroidRuntime(9968): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 00:59:35.352: E/AndroidRuntime(9968): at java.lang.reflect.Method.invoke(Method.java:511)
07-10 00:59:35.352: E/AndroidRuntime(9968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-10 00:59:35.352: E/AndroidRuntime(9968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-10 00:59:35.352: E/AndroidRuntime(9968): at dalvik.system.NativeStart.main(Native Method)
07-10 00:59:35.352: E/AndroidRuntime(9968): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.interrail.railrehber.AnalyticsSampleApp
07-10 00:59:35.352: E/AndroidRuntime(9968): at com.interrail.railrehber.MainActivity.onCreate(MainActivity.java:143)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.Activity.performCreate(Activity.java:5207)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-10 00:59:35.352: E/AndroidRuntime(9968): ... 11 more
AnalyticsSampleApp 页面代码为
package com.interrail.railrehber;
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import java.util.HashMap;
public class AnalyticsSampleApp extends Application {
// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID = "UA-56237196-12";
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.
}
public HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
public AnalyticsSampleApp() {
super();
}
public synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID): (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker): analytics.newTracker(R.xml.ecommerce_tracker);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}
}
我不明白为什么它总是崩溃。
你应该仔细阅读你的堆栈跟踪:
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.interrail.railrehber.AnalyticsSampleApp
在您的情况下,这意味着在这一行中:
Tracker t = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
getApplication()
returns Application
对象而不是 AnalyticsSampleApp
的对象。这很可能是由于您的清单 <application>
中缺少对 AnalyticsSampleApp
的引用(您需要 android:name
指向您的 class、see docs)。结果默认 Application
被您的应用程序使用而不是您的应用程序 class 并且转换异常只是此结果,因为您不能将父 class 转换为子 class.
我在 eclipse 上有一个项目,现在我尝试添加这个项目 Google Analytics,但是当我添加那个代码时,我的 MainActivity 应用程序崩溃了。
Tracker t = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
t.setScreenName("HomePage");
t.send(new HitBuilders.AppViewBuilder().build());
LogCat
07-10 00:59:35.352: E/AndroidRuntime(9968): FATAL EXCEPTION: main
07-10 00:59:35.352: E/AndroidRuntime(9968): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.interrail.railrehber/com.interrail.railrehber.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.interrail.railrehber.AnalyticsSampleApp
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.ActivityThread.access0(ActivityThread.java:141)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.os.Looper.loop(Looper.java:137)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-10 00:59:35.352: E/AndroidRuntime(9968): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 00:59:35.352: E/AndroidRuntime(9968): at java.lang.reflect.Method.invoke(Method.java:511)
07-10 00:59:35.352: E/AndroidRuntime(9968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-10 00:59:35.352: E/AndroidRuntime(9968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-10 00:59:35.352: E/AndroidRuntime(9968): at dalvik.system.NativeStart.main(Native Method)
07-10 00:59:35.352: E/AndroidRuntime(9968): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.interrail.railrehber.AnalyticsSampleApp
07-10 00:59:35.352: E/AndroidRuntime(9968): at com.interrail.railrehber.MainActivity.onCreate(MainActivity.java:143)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.Activity.performCreate(Activity.java:5207)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-10 00:59:35.352: E/AndroidRuntime(9968): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-10 00:59:35.352: E/AndroidRuntime(9968): ... 11 more
AnalyticsSampleApp 页面代码为
package com.interrail.railrehber;
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import java.util.HashMap;
public class AnalyticsSampleApp extends Application {
// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID = "UA-56237196-12";
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.
}
public HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
public AnalyticsSampleApp() {
super();
}
public synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID): (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker): analytics.newTracker(R.xml.ecommerce_tracker);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}
}
我不明白为什么它总是崩溃。
你应该仔细阅读你的堆栈跟踪:
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.interrail.railrehber.AnalyticsSampleApp
在您的情况下,这意味着在这一行中:
Tracker t = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
getApplication()
returns Application
对象而不是 AnalyticsSampleApp
的对象。这很可能是由于您的清单 <application>
中缺少对 AnalyticsSampleApp
的引用(您需要 android:name
指向您的 class、see docs)。结果默认 Application
被您的应用程序使用而不是您的应用程序 class 并且转换异常只是此结果,因为您不能将父 class 转换为子 class.