0

所以,我按照这个(http://java.dzone.com/articles/working-google-analytics-api)教程将 Google Analytics v4 添加到我的应用程序中,但是当我尝试运行它时,我得到了一个看起来像这样的错误:

09-13 12:52:01.122  31624-31624/com.rivert.unlockscope E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.rivert.unlockscope, PID: 31624
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rivert.unlockscope/com.rivert.unlockscope.unlockscope}: java.lang.ClassCastException: android.app.Application cannot be cast to com.rivert.unlockscope.MyApplication
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2187)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
        at android.app.ActivityThread.access$800(ActivityThread.java:138)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5034)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.rivert.unlockscope.MyApplication
        at com.rivert.unlockscope.unlockscope.onCreate(unlockscope.java:37)
        at android.app.Activity.performCreate(Activity.java:5241)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2151)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
        at android.app.ActivityThread.access$800(ActivityThread.java:138)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5034)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
        at dalvik.system.NativeStart.main(Native Method)
        09-13 12:52:02.594  31624-31624/com.rivert.unlockscope I/Process﹕ Sending signal. PID: 31624 SIG: 9

我第 37 行的代码是:

((MyApplication) getApplication()).getTracker(MyApplication.TrackerName.APP_TRACKER);

谁能帮我?谢谢

乐:

我的应用程序代码

package com.rivert.unlockscope;

import android.app.Application;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;

import java.util.HashMap;

/**
 * Created by Coman Alexandru on 12/09/2014.
 */
public class MyApplication extends Application {

// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID = "UA-XXXXXXXX-X";

//Logging TAG
private static final String TAG = "TestName";

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(R.xml.app_tracker)
                : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(PROPERTY_ID)
                : analytics.newTracker(R.xml.ecommerce_tracker);
        mTrackers.put(trackerId, t);

    }
    return mTrackers.get(trackerId);
}

}

我的日志:

http://pastebin.com/a2iidewj

4

2 回答 2

1

我以前也遇到过同样的问题。您需要在清单文件中添加应用程序名称。

 <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:name=".MainApplication" >
 </application>
于 2014-12-21T07:22:00.153 回答
1

事实上,您必须将您的 android:name 设置为您创建的扩展应用程序的类。例如,如果您的班级是:

public class Thisnameinmanifestconfig extends Application{
    private Tracker mTracker;
    synchronized public Tracker getDefaultTracker() {
        if(mTracker == null){
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            mTracker = analytics.newTracker(R.xml.global_tracker);
    }
    return mTracker;
}

您的清单应该是:

<application
    android:name=".Thisnameinmanifestconfig"
    .../>
</application>

而已。我希望它有所帮助。

于 2015-09-01T21:29:47.317 回答