简体   繁体   English

Google Analytics v4无法转换为Analytics

[英]Google Analytics v4 cannot be cast to Analytics

I try to implement the Google Analytics in my App. 我尝试在我的应用中实施Google Analytics(分析) The Problem is I get in LogCat the message: 问题是我在LogCat收到消息:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.name.appname/com.name.appname.activity.MainActivity}: java.lang.ClassCastException: com.name.appname.misc.AppApplication cannot be cast to com.name.appname.activity.Analytics 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 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: 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);
}
}

manifest: 表现:

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

In my manifest i have already declared android:name=".misc.AppApplication" at application. 在清单中,我已经在应用程序中声明了android:name=".misc.AppApplication" there is my check for PremiumUsers . 这是我的PremiumUsers

Here is my misc.AppApplication: 这是我的misc.AppApplication:

import android.app.Application;

public class AppApplication extends Application {

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

where is the problem? 问题出在哪儿?

You are setting the name attribute of your application element in the manifest to ".misc.AppApplication". 您将清单中应用程序元素的名称属性设置为“ .misc.AppApplication”。 That instructs Android to instantiate .misc.AppApplication class as application instead of the default Application. 指示Android将.misc.AppApplication类实例化为应用程序,而不是默认应用程序。 In your MainActivity class you try to cast .misc.AppApplication to Analytics and that will throw ClassCastException. 在MainActivity类中,您尝试将.misc.AppApplication强制转换为Analytics,这将引发ClassCastException。

The correct setup is to replace the Application class with Analytics (the class that extends Android Application class) and keep MainActivity as the class implementing your main activity. 正确的设置是用Google Analytics(分析)(扩展Android应用程序类的类)替换Application类,并将MainActivity保留为实现您的主要活动的类。

You should also not be logging events from onCreate override. 您也不应通过onCreate覆盖记录事件。 Activity can be created for reasons other then starting the app. 可以出于启动应用程序以外的其他原因创建活动。 For example when the device changes from landscape to portrait mode Android will ask the activity to save its state to a bundle, tear it down and recreate it passing the saved state in savedInstanceState. 例如,当设备从横向模式更改为纵向模式时,Android将要求活动将其状态保存为捆绑包,将其拆解并通过在saveInstanceState中保存的状态来重新创建它。 onCreate will be called in this case again. 在这种情况下,将再次调用onCreate。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM