[英]java.lang.RuntimeException: Unable to create service
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
How can i fix this crash? 我怎么能解决这个崩溃? I know it's caused by Google Analytics but I dont understand why I have followed the proper guide from google on how to implement it!
我知道这是由谷歌分析引起的,但我不明白为什么我遵循谷歌的正确指南如何实现它!
Class with Analytics 分析类
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());
}
So I initialize the tracker and the tracker properties are in a separate XML file but it's acting like I'm trying to initialize it two times or something and I'm not too sure as to why its happening! 所以我初始化跟踪器和跟踪器属性是在一个单独的XML文件中,但它的行为就像我正在尝试初始化它两次或者其他东西而且我不太确定它为什么会发生! Thanks allot for the help!
谢谢你的帮助!
Try moving initialize
to app class, so it will be called once in app life cycle. 尝试将
initialize
移动到app类,因此它将在应用程序生命周期中调用一次。
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
AnalyticsTrackers.initialize(this);
}
}
You need initializer the tracker instance into onCreate
method. 您需要将跟踪器实例初始化为
onCreate
方法。
AnalyticsApplication application = (AnalyticsApplication) getApplication();
myTracker = application.getDefaultTracker();
And then override the onResume
method to set tracker. 然后重写
onResume
方法以设置跟踪器。
myTracker.setScreenName("ScreenName" + name);
myTracker.send(new HitBuilders.ScreenViewBuilder().build());
The initialize method checks if the variable mInstance
is null, and if it isn't, it throws an exception. initialize方法检查变量
mInstance
是否为null,如果不是,则抛出异常。 Well, you break your program because you give mInstance
a non- null
value before initializing. 好吧,你打破你的程序,因为你在初始化之前给
mInstance
一个非null
值。 Don't do that. 不要那样做。
Look at your own code: 看看你自己的代码:
mInstance = this; //Don't do that
AnalyticsTrackers.getInstance().get(AnalyticsTrackers.Target.APP);
AnalyticsTrackers.initialize(this); //☠
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.