问题
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 :
- remove jar from project.
- Open SDK manager and > in extras download and install Google Play Service and Google Repository
- 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