Android google analytics integration error

不羁的心 提交于 2019-12-19 05:57:23

问题


When i am trying to get tracker in my activity it show error that -this method is undefine "getactivity()" in google analytic v4

// Get tracker.
    Tracker t = ((AnalyticsSampleApp) getActivity().getApplication())
            .getTracker(TrackerName.APP_TRACKER);

回答1:


If you haven't done so already, create a class MyApplication extends Application for your app, and make sure you add it to your manifest as below (the property that matters here is android:name, I've removed the other xml properties for clarity).

<application
    android:name="mypackagename.MyApplication"
    ... >

Then, in your MyApplication class, create a method getTracker as per Google Analytics v4 documentation https://developers.google.com/analytics/devguides/collection/android/v4/#tracking-methods

Then, use

Tracker t = ((MyApplication) getApplication())
        .getTracker(TrackerName.APP_TRACKER);



回答2:


I too faced this issue a little while back. To solve it I, as mentioned above, removed getActivity() as well as adding .MyApplication to android:name in the manifest. However I needed to add MyApplication before TrackerName as shown below.

Tracker t = ((MyApplication) getApplication()).getTracker(MyApplication.TrackerName.APP_TRACKER);

Hope this helps



来源:https://stackoverflow.com/questions/24950341/android-google-analytics-integration-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!