简体   繁体   中英

Google Anaytlics v4 Android casting error

I Cannot for some reason get the Anaytics simple screen view tracking to work, I have followed all the documentation, however am getting a casting issue:

This is how I am trying to call the code within my onCreate():

Tracker t = ((Analytics) getApplication()).getTracker(Analytics.TrackerName.APP_TRACKER);
t.setScreenName("Home");
t.send(new HitBuilders.AppViewBuilder().build());

However whenever I try to run it I get a casting error, please see below for my Log error message:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.test/com.test.test.Menu}: java.lang.ClassCastException: android.app.Application cannot be cast to com.test.test.Analytics

Edit - Another attempt:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="ga_sessionTimeout">300</integer>
    <bool name="ga_autoActivityTracking">true</bool>
    <screenName name="com.test.test.Menu">
        Analytics Menu
    </screenName>
    <!--  The following value should be replaced with correct property id. -->
    <string name="ga_trackingId">UA-XXXXXXX-1</string> <!-- I have added my actual trackid in my code -->
</resources>

// Then I use this within my Menu.onCreate()
 ((Analytics) getApplication()).getTracker(Analytics.TrackerName.APP_TRACKER);

However I am still getting the same casting error stated above

It looks like your error has nothing to do with Analytics. Check if you have replaced the Application class in your manifest with com.test.test.Analytics. It seems you are still using the default Android application class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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