gpt4 book ai didi

android - 当我添加谷歌分析时,发生classcastException

转载 作者:行者123 更新时间:2023-12-03 17:18:45 25 4
gpt4 key购买 nike

现在,我在我的应用程序中添加了Google Analytics(分析)。

我已经完成了Google Analytics(分析)首页的所有操作。
https://developers.google.com/analytics/devguides/collection/android/v4/#manifest

但是在我在MainActivity中添加Tracker代码后,运行我的应用程序时会发生ClassCastException。

请给我一个答案。

我已经在搜索谷歌和堆栈溢出。

后面是我的代码。

ApplicationTrackers.java



package com.zla.android;

import java.util.HashMap;
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import com.zla.android.R;

public class ApplicationTrackers extends Application {

private static final String PROPERTY_ID = "UA-XXXXXXXX-2";

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.
}
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

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);
}
}





global_tracker.xml



<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:ignore="TypographyDashes">

<integer name="ga_sessionTimeout">300</integer>

<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>

<!-- The screen names that will appear in reports -->
<screenName name="com.zla.android.MainActivity">
MainActivity
</screenName>

<string name="ga_trackingId">UA-XXXXXXXX-2</string>
</resources>





ecommerce_tracker.xml



<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="ga_sessionTimeout">300</integer>
<string name="ga_trackingId">UA-XXXXXXXX-2</string>
</resources>





MainActivity.java



package com.zla.android;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


/* add google analytics --------------------------------------------- */
Tracker t = ((ApplicationTrackers)getApplication()).getTracker(ApplicationTrackers.TrackerName.APP_TRACKER);
t.setScreenName("MainActivity");
t.send(new HitBuilders.AppViewBuilder().build());
/* ------------------------------------------------------------------ */





androidManifest.xml



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zla.android"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />

<!-- about google analytics -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:largeHeap="true" >

<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="com.zla.android.SearchBrandActivity"
android:label="search page" />

<activity
android:name="com.zla.android.SearchFrameActivity"
android:label="search page" />

<activity
android:name="com.zla.android.ListActivity"
android:label="list page"
android:parentActivityName="com.zla.android.MainActivity" >
</activity>

<activity
android:name="com.zla.android.ListActivity_WishList"
android:label="list page"
android:parentActivityName="com.zla.android.MainActivity" >
</activity>

<activity
android:name="com.zla.android.DetailActivity"
android:label="detail page"
android:parentActivityName="com.zla.android.ListActivity" />

<!-- about google analytics -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<activity
android:name="com.zla.android.ApplicationTrackers"
android:label="google analytics" />

</application>

</manifest>





错误的LogCat



04-05 03:27:42.498: E/AndroidRuntime(22322): FATAL EXCEPTION: main
04-05 03:27:42.498: E/AndroidRuntime(22322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zla.android/com.zla.android.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.zla.android.ApplicationTrackers
04-05 03:27:42.498: E/AndroidRuntime(22322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
04-05 03:27:42.498: E/AndroidRuntime(22322): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
04-05 03:27:42.498: E/AndroidRuntime(22322): at android.app.ActivityThread.access$700(ActivityThread.java:143)
04-05 03:27:42.498: E/AndroidRuntime(22322): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
04-05 03:27:42.498: E/AndroidRuntime(22322): at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 03:27:42.498: E/AndroidRuntime(22322): at android.os.Looper.loop(Looper.java:137)
04-05 03:27:42.498: E/AndroidRuntime(22322): at android.app.ActivityThread.main(ActivityThread.java:4950)
04-05 03:27:42.498: E/AndroidRuntime(22322): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 03:27:42.498: E/AndroidRuntime(22322): at java.lang.reflect.Method.invoke(Method.java:511)
04-05 03:27:42.498: E/AndroidRuntime(22322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997)
04-05 03:27:42.498: E/AndroidRuntime(22322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
04-05 03:27:42.498: E/AndroidRuntime(22322): at dalvik.system.NativeStart.main(Native Method)
04-05 03:27:42.498: E/AndroidRuntime(22322): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.zla.android.ApplicationTrackers
04-05 03:27:42.498: E/AndroidRuntime(22322): at com.zla.android.MainActivity.onCreate(MainActivity.java:67)
04-05 03:27:42.498: E/AndroidRuntime(22322): at android.app.Activity.performCreate(Activity.java:5177)
04-05 03:27:42.498: E/AndroidRuntime(22322): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-05 03:27:42.498: E/AndroidRuntime(22322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
04-05 03:27:42.498: E/AndroidRuntime(22322): ... 11 more

最佳答案

您没有将com.zla.android.ApplicationTrackers用作应用程序实现。您需要在类名称扩展为android.app.Application的AndroidManifest.xml中的application元素中添加"name"属性。

<application
...
android:name="com.zla.android.ApplicationTrackers">
...


没有name属性,Android无法知道哪个类实现了该应用程序,并将实例化默认的 android.app.Application。当您以后尝试将其强制转换为自定义应用程序类时,由于尝试将 android.app.Application强制转换为 com.zla.android.ApplicationTrackers,因此转换失败。

关于android - 当我添加谷歌分析时,发生classcastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29450111/

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