我尝试在我的应用中实施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>


主要活动:

@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;
}
}


问题出在哪里?

最佳答案

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

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

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

关于java - Google Analytics v4无法转换为Analytics,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30415587/

10-09 09:24