gpt4 book ai didi

java - Android - 谷歌分析崩溃

转载 作者:行者123 更新时间:2023-11-30 02:01:56 25 4
gpt4 key购买 nike

我在 eclipse 上有一个项目,现在我尝试将这个项目添加到 Google Analytics,但是当我添加那个代码时,我的 MainActivity 应用程序崩溃了。

  Tracker t = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
t.setScreenName("HomePage");
t.send(new HitBuilders.AppViewBuilder().build());

日志猫

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.access$600(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()返回 Application对象而不是 AnalyticsSampleApp的。这很可能是由于缺少对 AnalyticsSampleApp 的引用引起的在您的 list 中 <application> (你需要 android:name 指向你的类(class), see docs )。结果默认 Application由您的应用程序而不是您的类使用,并且转换异常只是此结果,因为您不能将父类转换为子类。

关于java - Android - 谷歌分析崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31329031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com