GreenRobot 异常:de.greenrobot.event.EventBusException:调用订户失败

GreenRobot Exception : de.greenrobot.event.EventBusException: Invoking subscriber failed

我时常遇到这个异常。

我只是以标准方式使用 green-robot,在视图、片段、活动、服务和应用程序之间使用默认实例,并且不时使用一些粘性事件。

我没有找到与此异常相关的任何其他 post。有什么想法或提示可以开始我的调查吗?

事件总线运行良好(约 20 个事件,10 个订阅者),一切都是用户触发的,因此现场工作量不大。

完整的堆栈跟踪在这里:

de.greenrobot.event.EventBusException: Invoking subscriber failed
10-27 15:37:00.522 25414-25414/fr.tech.app..u E/AndroidRuntime:     at de.greenrobot.event.EventBus.handleSubscriberException(EventBus.java:518)
10-27 15:37:00.522 25414-25414/fr.tech.app..u E/AndroidRuntime:     at de.greenrobot.event.EventBus.invokeSubscriber(EventBus.java:500)
10-27 15:37:00.522 25414-25414/fr.tech.app..u E/AndroidRuntime:     at de.greenrobot.event.EventBus.postToSubscription(EventBus.java:429)
10-27 15:37:00.522 25414-25414/fr.tech.app..u E/AndroidRuntime:     at de.greenrobot.event.EventBus.postSingleEventForEventType(EventBus.java:410)
10-27 15:37:00.522 25414-25414/fr.tech.app..u E/AndroidRuntime:     at de.greenrobot.event.EventBus.postSingleEvent(EventBus.java:383)
10-27 15:37:00.522 25414-25414/fr.tech.app..u E/AndroidRuntime:     at de.greenrobot.event.EventBus.post(EventBus.java:263)
10-27 15:37:00.522 25414-25414/fr.tech.app..u E/AndroidRuntime:     at fr.u.app.u.Dialog.TastingNavigationDialog.onSelection(TastingNavigationDialog.java:42)

错误是由 MaterialDialog 实例触发的:

  dialogBuilder = new MaterialDialog.Builder(context)
            .title(R.string.dialogTastingNavigationTripTitle)
            .negativeText(R.string.buttonCancel)
            .cancelable(false)
            .adapter(listAdapter, new MaterialDialog.ListCallback() {
                @Override
                public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                    EventBus.getDefault().post(new TastingPageToEvent(listAdapter.list.get(which), which));
                    dialog.dismiss();
                }
            });

编辑

我发现一件事触发了异常,post从一个片段中调用一个 stickyEvent。目的是让一个即将出现的片段能够恢复那个粘性事件。

当转到 Eventbus 源时,它停在:

   void invokeSubscriber(Subscription subscription, Object event) {
        try {
            subscription.subscriberMethod.method.invoke(subscription.subscriber, event);
        } catch (InvocationTargetException e) {
            handleSubscriberException(subscription, event, e.getCause());
        } catch (IllegalAccessException e) {
            throw new IllegalStateException("Unexpected exception", e);
        }
    }

编辑

这是场景。

public class TopEvent {
    String name;
    public TopEvent(String name){
        this.name = name;
    }
}

public class MyEvent extends TopEvent {
    String extraInfo;
    public MyEvent(String name, String extra){
        super(name);
        this.extraInfo = extra; 
    }
}

这是一个片段 Tx :

...
EventBus.getDefault().postStickyEvent(new MyEvent("Stack","Overflow");
...

这是第二个 Fragment Rx

...
String extra = EventBus.getDefault().getStickyEvent(MyEvent.class).extraInfo;
...

这是一项服务(出现奇怪行为的服务)

public class MyService extends Service {

    ...

    EventBus.getDefault().registerSticky(this)

    onEvent(TopEvent event){
       ...
       ...
    }

}

在 onEvent 结束时,抛出异常。好像它们是一些隐藏的行为,带有粘性扩展事件(来自超级)posted 触发超级事件的非粘性事件。

这是由于 EventBus 配置所致:

EventBus.builder().throwSubscriberException(BuildConfig.DEBUG).installDefaultEventBus();

但是我很难理解为什么这个选项很有趣。事实上,我们(几乎)只看到失败的订阅者的异常,因为有另一个订阅者的异常。但它往往是隐藏的。那么为什么这很有趣?

您必须检查捕获您的事件的 onEvent 方法那里发生了错误并且事件总线抛出此异常。