代码之家  ›  专栏  ›  技术社区  ›  ChaimKut

来自Google Analytics for Android的RuntimeException“向死线程上的处理程序发送消息”

  •  6
  • ChaimKut  · 技术社区  · 14 年前

    我正在使用googleanalyticsforandroid跟踪Android应用程序上的事件、页面浏览量。调用跟踪器的分派方法时,我收到一个RuntimeException:

    07-12 18:02:05.594: WARN/MessageQueue(12823): Handler{44a08620} sending message to a Handler on a dead thread
    07-12 18:02:05.594: WARN/MessageQueue(12823): java.lang.RuntimeException: Handler{44a08620} sending message to a Handler on a dead thread
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at android.os.MessageQueue.enqueueMessage(MessageQueue.java:179)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at android.os.Handler.sendMessageAtTime(Handler.java:457)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at android.os.Handler.sendMessageDelayed(Handler.java:430)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at android.os.Handler.post(Handler.java:248)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at com.google.android.apps.analytics.NetworkDispatcher$DispatcherThread.dispatchEvents(Unknown Source)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at com.google.android.apps.analytics.NetworkDispatcher.dispatchEvents(Unknown Source)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at com.google.android.apps.analytics.GoogleAnalyticsTracker.dispatch(Unknown Source)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at com.company.activity.set(MyActivity.java:177)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at com.company.activity.access$34(MyActivity.java:175)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at com.company.activity.$45.onClick(MyActivity.java:1982)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at android.os.Looper.loop(Looper.java:123)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at java.lang.reflect.Method.invoke(Method.java:521)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    07-12 18:02:05.594: WARN/MessageQueue(12823):     at dalvik.system.NativeStart.main(Native Method)
    

    --编辑-- Google's own example code (也可在SDK下载中获得)也会引发此异常:

    >07-12 23:47:09.343: WARN/MessageQueue(18468): null sending message to a Handler on a dead thread
    07-12 23:47:09.343: WARN/MessageQueue(18468): java.lang.RuntimeException: null sending message to a Handler on a dead thread
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at android.os.MessageQueue.enqueueMessage(MessageQueue.java:179)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at android.os.Looper.quit(Looper.java:173)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at com.google.android.apps.analytics.NetworkDispatcher.stop(Unknown Source)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at com.google.android.apps.analytics.NetworkDispatcher.init(Unknown Source)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at com.google.android.apps.analytics.GoogleAnalyticsTracker.start(Unknown Source)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at com.google.android.apps.analytics.GoogleAnalyticsTracker.start(Unknown Source)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at com.google.android.apps.analytics.GoogleAnalyticsTracker.start(Unknown Source)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at com.google.android.apps.analytics.sample.AnalyticsSample.onCreate(AnalyticsSample.java:23)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3815)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at android.app.ActivityThread.access$2400(ActivityThread.java:125)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2037)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at android.os.Looper.loop(Looper.java:123)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at java.lang.reflect.Method.invoke(Method.java:521)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    07-12 23:47:09.343: WARN/MessageQueue(18468):     at dalvik.system.NativeStart.main(Native Method)
    

    当您强制销毁原始活动(即更改方向、转到主屏幕等)时,会发生这种情况。我无法通过onSavedInstance存储原始跟踪器对象,因为跟踪器不是可包裹的。有没有其他的想法,线索,如何摆脱这个错误?

    --编辑-- 更奇怪的是,这似乎也导致了以下问题:

    >07-13 00:50:02.581: WARN/googleanalytics(27605): Dispatcher thinks it finished, but there were -4 failed events
    

    虽然大多数人提到这一点,他们的手指回到事件或页面视图中的空白,破坏数据库,我已经证实这不是事实。另外,我也不知道负面事件(-4)意味着什么!

    1 回复  |  直到 13 年前
        1
  •  1
  •   maxcanna    13 年前

    你可以在这里做一个调度 onPause()

    tracker.dispatch();
    

    以避免丢失数据,然后重新创建跟踪器