我正在尝试使用Google Analytics跟踪我的应用统计信息。我正在尝试按照http://java.dzone.com/articles/working-google-analytics-api上的教程进行操作,其中有几个人发布了这个教程并说它适用于他们。这是我的代码:
我的app_tracker.xml文件位于
之下<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="TypographyDashes">
<span style="line-height: 1.5; font-style: inherit; font-weight: inherit;">
</span>
<string name="ga_trackingId">UA-XXXXXXX-1</string>
<string name="ga_sampleFrequency">100.0</string>
<bool name="ga_autoActivityTracking">true</bool>
<bool name="ga_reportUncaughtExceptions">true</bool>
<integer name="ga_sessionTimeout">-1</integer>
<screenName name="game">game</screenName>
<screenName name="settings">settings</screenName>
</resources>
我的global_tracker xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="TypographyDashes">
<span style="line-height: 1.5; font-style: inherit; font-weight: inherit;">
</span>
<string name="ga_logLevel">verbose</string>
<integer name="ga_dispatchPeriod">30</integer>
<bool name="ga_dryRun">false</bool>
<string name="games">games</string>
<string name="settings">settings</string>
</resources>
在我的清单文件中,我输入了以下代码块:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
我的主要活动如下:
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
public class MainActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((MyApplication)getApplication()).getTracker(MyApplication.TrackerName.APP_TRACKER);
}
应用程序应具有的MyApplication类如下
public class MyApplication extends Application {
private static final String PROPERTY_ID = "UA-XXXXXXXX-1";
private static final String TAG = "MyApp";
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.
}
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
public MyApplication() {
super();
}
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(PROPERTY_ID)
: analytics.newTracker(R.xml.ecommerce_tracker);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}
}
当程序运行时,它到达行
时崩溃((MyApplication)getApplication()).getTracker(MyApplication.TrackerName.APP_TRACKER);
在主要活动中。我花了几周试图找到解决方法,但没有成功。有人可以帮我在我的android项目中配置google analytics v4吗?
我正在编辑此内容(因为我遇到同样的问题)&amp;这里是logcat
:10-29 15:31:13.062: E/AndroidRuntime(683): FATAL EXCEPTION: main
10-29 15:31:13.062: E/AndroidRuntime(683): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mrana.send/com.mrana.send.HomeActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.mrana.send.SendApplication
10-29 15:31:13.062: E/AndroidRuntime(683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-29 15:31:13.062: E/AndroidRuntime(683): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-29 15:31:13.062: E/AndroidRuntime(683): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-29 15:31:13.062: E/AndroidRuntime(683): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-29 15:31:13.062: E/AndroidRuntime(683): at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 15:31:13.062: E/AndroidRuntime(683): at android.os.Looper.loop(Looper.java:137)
10-29 15:31:13.062: E/AndroidRuntime(683): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-29 15:31:13.062: E/AndroidRuntime(683): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 15:31:13.062: E/AndroidRuntime(683): at java.lang.reflect.Method.invoke(Method.java:511)
10-29 15:31:13.062: E/AndroidRuntime(683): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-29 15:31:13.062: E/AndroidRuntime(683): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-29 15:31:13.062: E/AndroidRuntime(683): at dalvik.system.NativeStart.main(Native Method)
10-29 15:31:13.062: E/AndroidRuntime(683): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.mrana.send.SendApplication
10-29 15:31:13.062: E/AndroidRuntime(683): at com.mrana.send.HomeActivity.onCreate(HomeActivity.java:73)
10-29 15:31:13.062: E/AndroidRuntime(683): at android.app.Activity.performCreate(Activity.java:5008)
10-29 15:31:13.062: E/AndroidRuntime(683): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-29 15:31:13.062: E/AndroidRuntime(683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-29 15:31:13.062: E/AndroidRuntime(683): ... 11 more
第73行与op中提到的崩溃应用程序的行相同。
注意:我在模拟器上测试它。此功能是否需要在真实设备上进行测试(安装了Play商店)?
答案 0 :(得分:0)
您需要在清单文件中添加应用程序名称。
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name=".MainApplication" >
</application>