java.lang.IllegalStateException: Extra call to initialize analytics trackers
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2801)
at android.app.ActivityThread.access$1800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.IllegalStateException: Extra call to initialize analytics trackers
at com.apps.skytek.notify.AnalyticsTrackers.initialize(AnalyticsTrackers.java:32)
at com.apps.skytek.notify.NotificationService.onCreate(NotificationService.java:51)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2791)
... 9 more
我该如何修复这次崩溃?我知道这是由谷歌分析引起的,但我不明白为什么我遵循谷歌关于如何实施它的正确指南!
分析课程
public void onCreate() {
super.onCreate();
context = getApplicationContext();
mNotificationManager = (NotificationManager) getSystemService("notification");
mInstance = this;
AnalyticsTrackers.getInstance().get(AnalyticsTrackers.Target.APP);
AnalyticsTrackers.initialize(this);
}
public static synchronized NotificationService getInstance() {
return mInstance;
}
public synchronized Tracker getGoogleAnalyticsTracker() {
AnalyticsTrackers analyticsTrackers = AnalyticsTrackers.getInstance();
return analyticsTrackers.get(AnalyticsTrackers.Target.APP);
}
/***
* Tracking screen view
*
* @param screenName screen name to be displayed on GA dashboard
*/
public void trackScreenView(String screenName) {
Tracker t = getGoogleAnalyticsTracker();
// Set screen name.
t.setScreenName(screenName);
// Send a screen view.
t.send(new HitBuilders.ScreenViewBuilder().build());
GoogleAnalytics.getInstance(this).dispatchLocalHits();
}
/***
* Tracking exception
*
* @param e exception to be tracked
*/
public void trackException(Exception e) {
if (e != null) {
Tracker t = getGoogleAnalyticsTracker();
t.send(new HitBuilders.ExceptionBuilder()
.setDescription(
new StandardExceptionParser(this, null)
.getDescription(Thread.currentThread().getName(), e))
.setFatal(false)
.build()
);
}
}
/***
* Tracking event
*
* @param category event category
* @param action action of the event
* @param label label
*/
public void trackEvent(String category, String action, String label) {
Tracker t = getGoogleAnalyticsTracker();
// Build and send an Event.
t.send(new HitBuilders.EventBuilder().setCategory(category).setAction(action).setLabel(label).build());
}
所以我初始化跟踪器和跟踪器属性是在一个单独的XML文件中,但它的行为就像我试图将它初始化两次或者其他东西而且我不太确定它为什么会发生!谢谢你的帮助!
答案 0 :(得分:1)
尝试将initialize
移至app类,以便在应用生命周期中调用一次。
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
AnalyticsTrackers.initialize(this);
}
}
答案 1 :(得分:0)
您需要将跟踪器实例初始化为onCreate
方法。
AnalyticsApplication application = (AnalyticsApplication) getApplication();
myTracker = application.getDefaultTracker();
然后覆盖onResume
方法以设置跟踪器。
myTracker.setScreenName("ScreenName" + name);
myTracker.send(new HitBuilders.ScreenViewBuilder().build());
答案 2 :(得分:-1)
initialize方法检查变量mInstance
是否为空,如果不是,则抛出异常。好吧,你打破了你的程序,因为在初始化之前你给了mInstance
一个非null
的值。不要那样做。
查看您自己的代码:
mInstance = this; //Don't do that
AnalyticsTrackers.getInstance().get(AnalyticsTrackers.Target.APP);
AnalyticsTrackers.initialize(this); //☠