我正在尝试在我的应用中设置Google Analytics。我按照下面的谷歌教程 https://developers.google.com/analytics/devguides/collection/android/v4/
但得到了以下错误
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to
当我将下面的代码放入MainActivity中的create:
时 AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
AnalyticsApplication类:
import android.app.Application;
导入com.google.android.gms.analytics.GoogleAnalytics; 导入com.google.android.gms.analytics.Tracker;
公共类AnalyticsApplication扩展了Application {
private Tracker mTracker;
synchronized public Tracker getDefaultTracker() {
if (mTracker == null) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
// To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
mTracker = analytics.newTracker(R.xml.global_tracker);
}
return mTracker;
}
}
GRADLE PROJECT
classpath 'com.google.gms:google-services:1.5.0-beta2'
GRADLE MODULE
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services-ads:8.3.0'
compile 'com.google.android.gms:play-services-analytics:8.3.0'
compile 'com.google.android.gms:play-services-appindexing:8.3.0'
} 申请插件:' com.google.gms.google-services'
AndroidMaifest
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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>
<service
android:name=".CallDetectService"
android:enabled="true"
android:exported="false" >
</service>
<receiver
android:name=".RingerModeStateReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.media.RINGER_MODE_CHANGED" />
</intent-filter>
</receiver>
<!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
我也将配置文件复制到app目录中。 如果我省略主要活动中的代码我的应用程序将运行但没有任何显示 在我的Google Analytics帐户中。
非常感谢任何帮助。 我按照谷歌教程但没有发生任何事情然后我把代码放入主要活动我的应用程序将崩溃。
答案 0 :(得分:2)
要在Manifest文件中声明应用程序类,请使用android:name =&#34; com.example.AnalyticsApplication&#34;在应用程序标签中。
或使用分析助手类:
public class AnalyticsHelper {
private static HashMap<TrackerName, Tracker> sTrackers = new HashMap<TrackerName, Tracker>();
public static synchronized Tracker getTracker(Context context, TrackerName trackerId) {
if (!sTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(context);
Tracker t = null;
switch (trackerId) {
case APP_TRACKER:
t = analytics.newTracker(R.xml.analytics_app_tracker);
break;
}
sTrackers.put(trackerId, t);
}
return sTrackers.get(trackerId);
}
public enum TrackerName {
APP_TRACKER, // App specific tracker ID
}
}
并致电AnalyticsHelper.getTracker(this, TrackerName.APP_TRACKER)
您的Application类。