简体   繁体   中英

Android - Google Analytics V4 on FragmentActivity

I am trying to implement Google Analytics V4 in Android. I tried following Google doc , also I tried following this tutorial . My MainActivity extents FragmentActivity, I have added the GA lib, xml config files and made the modifications showed in the tutorial.

My problem is in the following line that shows me "Cannot resolve 'AnalyticsSampleApp' symbol" error.

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

I know that I have to change 'MyApplication' and use my own application class name, but how can I find my application class name? How can I register my application class name in my manifest file so I can use it?

I tried this..

     ...
<application
    android:name="my.package.myApplication" //Cannot resolve symbol 'myApplication'
     ...

Should I implement Google Analytics API in another way since my activity is extending FragmentActivity?

*Sorry if this is a silly question. I am very inexperienced in Android development.

"Cannot resolve 'AnalyticsSampleApp' symbol"

Because AnalyticsSampleApp you have not created AnalyticsSampleApp class in your application.

Create a class with MyApplication name and extend Application class:

public class MyApplication extends Application {
  public MyApplication() {
   super();
  }
  .....
}

Declare MyApplication in AndroidManifest.xml:

<application
    android:name="my.package.MyApplication"
 ....
 />

Now use MyApplication class name instead of to get Tracker instance:

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

As provided tutorial link you can follow MyApplication class code to create and initialize GoogleAnalytics class instace.

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