Emanuel Graf
Emanuel Graf

Reputation: 766

Trying to implement Google analytics, error tracker class cannot be resolved to a variable

I followed the instructions given by google and using the Sample Application. eclipse says in the MainScreen, MyApplication cannot be resolved to a variable, but if i ctrl+click it, it jumps to the MyApplication.java... what is wrong?

this is MyApplication.java

public class MyApplication extends Application{

    HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
    private static final String PROPERTY_ID = "UA-61709726-1";



    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.
    }

    public MyApplication() {
        super();
    }

      synchronized Tracker getTracker(TrackerName trackerId) {
            if (!mTrackers.containsKey(trackerId)) {

                GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
                analytics.getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
                Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
                        : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(
                                R.xml.global_tracker)
                                : analytics.newTracker(R.xml.ecommerce_tracker);
                t.enableAdvertisingIdCollection(true);

                mTrackers.put(trackerId, t);
            }
            return mTrackers.get(trackerId);



        }
}

and this is MainScreen.java

//...............
@Override
    public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


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

              // Set screen name.
              t.setScreenName("MainScreen");

              // Send a screen view.
              t.send(new HitBuilders.ScreenViewBuilder().build());

//................

My manifest:

 //................

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
       android:name=".MyApplication">  <!-- replace with your app class-->
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme" >

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"

 //................

Upvotes: 0

Views: 692

Answers (1)

djabi
djabi

Reputation: 5767

You need to let Android know that you want it to use your custom Application implementation instead of the default android.app.Application. To do so you need to add android:name="<your-app-class>" attribute to your AndroidManifest.xml:

<manifest>
  ...
  <application
    android:name=".MyApplication">  <!-- replace with your app class-->
    ...
  </application>
</manifest>

Upvotes: 1

Related Questions