现在我正在我的应用中添加谷歌分析。
我已经完成了谷歌分析主页。 (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
答案 0 :(得分:2)
您没有将com.zla.android.ApplicationTrackers
用作应用程序实现。您需要将"name"
属性添加到AndroidManifest.xml中的application元素,类名扩展为android.app.Application。
<application
...
android:name="com.zla.android.ApplicationTrackers">
...
如果没有name属性,Android无法知道哪个类实现了应用程序,并将实例化默认的android.app.Application
。当您稍后尝试将其强制转换为自定义应用程序类时,当您尝试将android.app.Application
强制转换为com.zla.android.ApplicationTrackers
时,强制转换会失败。
答案 1 :(得分:0)
Tracker t = getTracker(ApplicationTrackers.TrackerName.APP_TRACKER);
试试这个