我刚刚关注了this将 Google Analytics SDK v4 添加到 Android 应用程序的教程。现在的问题是,当我运行该应用程序时,它只会在我设置了跟踪的任何 View 上显示黑屏。例如,下面是闪屏后第一个 View 的 onCreate、onStart 和 onStop 方法。初始屏幕加载正常,然后我在 View 上看到黑屏。如果您注释掉分析代码,一切正常
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.landing_screen);
//Get a Tracker (should auto-report)
//Define font for use
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/cabinregular.otf");
//Set font to all elements
((TextView) findViewById(R.id.textFlat)).setTypeface(typeface);
((TextView) findViewById(R.id.textCool)).setTypeface(typeface);
((TextView) findViewById(R.id.textWarm)).setTypeface(typeface);
((TextView) findViewById(R.id.textPastels)).setTypeface(typeface);
((TextView) findViewById(R.id.textDecades)).setTypeface(typeface);
((TextView) findViewById(R.id.textNeutrals)).setTypeface(typeface);
((TextView) findViewById(R.id.textMidTones)).setTypeface(typeface);
//((TextView) findViewById(R.id.appHeaderText)).setTypeface(typeface);
//Create all the listeners to catch button presses
ImageButton buttonFlat = (ImageButton) findViewById(R.id.buttonFlat);
buttonFlat.setOnClickListener(this);
ImageButton buttonCool = (ImageButton) findViewById(R.id.buttonCool);
buttonCool.setOnClickListener(this);
ImageButton buttonWarm = (ImageButton) findViewById(R.id.buttonWarm);
buttonWarm.setOnClickListener(this);
ImageButton buttonPastels = (ImageButton) findViewById(R.id.buttonPastels);
buttonPastels.setOnClickListener(this);
ImageButton buttonDecades = (ImageButton) findViewById(R.id.buttonDecades);
buttonDecades.setOnClickListener(this);
ImageButton buttonNeutrals = (ImageButton) findViewById(R.id.buttonNeutrals);
buttonNeutrals.setOnClickListener(this);
ImageButton buttonMidTones = (ImageButton) findViewById(R.id.buttonMidTones);
buttonMidTones.setOnClickListener(this);
((colorlibApplicationClass) getApplication()).getTracker(colorlibApplicationClass.TrackerName.APP_TRACKER);
}
public void onStart()
{
super.onStart();
//Get an Analytics tracker to report app starts & uncaught exceptions etc.
GoogleAnalytics.getInstance(this).reportActivityStart(this);
}
public void onStop()
{
super.onStop();
//Stop the analytics tracking
GoogleAnalytics.getInstance(this).reportActivityStop(this);
}
这是应用程序类;
package com.exampleapp.colorlib;
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import java.util.HashMap;
public class colorlibApplicationClass extends Application {
// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID = "UA-REMOVED-FOR-SECURITY";
//Logging TAG
private static final String TAG = "MyApp";
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 the apps from a company. eg: roll-up tracking.
}
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
public colorlibApplicationClass() {
super();
}
synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(R.xml.app_tracker)
: (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(PROPERTY_ID)
: analytics.newTracker(R.xml.ecommerce_tracker);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}
}
最佳答案
是上个版本的问题。检查here了解更多信息。
与此同时,也许最好使用旧版本。这个工作正常:
编译 'com.google.android.gms:play-services:6.1.+'
编辑:从 AndroidManifest 中删除全局配置元数据工作正常。
关于Android 谷歌分析导致黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27423750/