cannot resolve method newTracker (Analytics)

廉价感情. 提交于 2019-12-25 02:55:15

问题


I'm trying to use google analytics in my project. and the newTracker method cannot be found. Also, I have no need for the ecommerce tracker so I took it out. But now i can't close the statement with getting the error

: is expected here''

I commented where the error shows up below.

import android.app.Application;

import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;

import java.util.HashMap;

public class AnalyticsTracker1 extends Application {

private static final String PROPERTY_ID = "UA-xxxxxxx-1";


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.

}


HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

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

        GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
        Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
                : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker); //: expected here

        mTrackers.put(trackerId, t);

    }
    return mTrackers.get(trackerId);
}
}

回答1:


Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
            : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker) : null;

Modify your code like this , if trackerID is not equal to any of enum then it should return null. Error is coming as you have not completed second ternary operator. For second problem I assume that you have imported google analytics jar into your project. If it is than it wont work. Solution in that case :

  1. remove jar from project.
  2. Open SDK manager and > in extras download and install Google Play Service and Google Repository
  3. When you are done, got to Google play service set up page at "Add Google Play Services to Your Project" select from dropdown Eclipse or Android Studio and follow the process and add Google Play service project as lib to your own project

4 . add

<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> to application tag of your manifest. and it will work.

I have checked its working now.



来源:https://stackoverflow.com/questions/29197771/cannot-resolve-method-newtracker-analytics

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