Google Analytics v4无法投放到Google Analytics

时间:2015-05-23 17:23:21

标签: java android google-analytics

我尝试在我的应用中实施 Google Analytics 。 问题是我收到了LogCat消息:

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.name.appname / com.name.appname.activity.MainActivity}:java.lang.ClassCastException:com.name.appname.misc.AppApplication无法强制转换到com.name.appname.activity.Analytics

global_tracker.xml

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

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

<!-- Enable verbose logging -->
<String name="ga_loglevel">verbose</String>

<!-- Screen names on the reports -->
<screenName name="com.name.appname.activity.MainActivity">
    MainActivity ScreenView
</screenName>

<!--  Tracking ID -->
<string name="ga_trackingId">UA-xxxxxxx-2</string>

ANALytics.java:

public class Analytics extends Application {

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


synchronized Tracker getTracker(TrackerName trackerId) {
    if (!mTrackers.containsKey(trackerId)) {

        GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
        Tracker t = analytics.newTracker(R.xml.global_tracker);
        mTrackers.put(trackerId, t);

    }
    return mTrackers.get(trackerId);
}
}

清单:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:name=".misc.AppApplication" >

       <activity
        android:name=".activity.Analytics"
        android:allowBackup="true"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
    </activity>

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    adView = (AdView) this.findViewById(R.id.adView);

    // Get tracker.
    Tracker tracker = ((Analytics)getApplication()).getTracker(Analytics.TrackerName.GLOBAL_TRACKER);
    tracker.send(new HitBuilders.EventBuilder()
       .setCategory("Use")
       .setAction("Use Programm")
       .setLabel("submit")
       .build());

@Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        GoogleAnalytics.getInstance(MainActivity.this).reportActivityStart(this);
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        GoogleAnalytics.getInstance(MainActivity.this).reportActivityStop(this);
    }

在我的清单中,我已经申请了android:name=".misc.AppApplication"。检查PremiumUsers

这是我的misc.AppApplication:

import android.app.Application;

public class AppApplication extends Application {

private boolean mIsPremium;
public void setPremium(){
    mIsPremium = true;
}
public boolean isPremium(){
    return mIsPremium;
}
}

问题出在哪里?

1 个答案:

答案 0 :(得分:1)

您正在将清单中的应用程序元素的name属性设置为“.misc.AppApplication”。这指示Android将.misc.AppApplication类实例化为应用程序而不是默认的Application。在您的MainActivity类中,您尝试将.misc.AppApplication强制转换为Analytics,这将抛出ClassCastException。

正确的设置是将Application类替换为Analytics(扩展Android Application类的类),并将MainActivity保持为实现主要活动的类。

您也不应该从onCreate覆盖中记录事件。可以出于启动应用程序之外的其他原因创建活动。例如,当设备从横向模式更改为纵向模式时,Android会要求活动将其状态保存到捆绑包中,将其拆除并重新创建它,并将已保存的状态传递给savedInstanceState。在这种情况下将再次调用onCreate。