我正在开发一个Android应用程序,而且我正在使用guava eventbus。 关于无法发送事件,我得到了模棱两可的错误。 我该如何调查这些问题?以及如何获得有关异常的更多信息?
这是一个示例异常消息:
04-12 20:46:35.829 9971-11208/xxx.android.init E/default﹕ Could not dispatch event: xxx.android.presentation.control.MainActivity@21139768 to public void xxx.android.presentation.control.MainActivity.showToast(xxx.core.event.EventShowToast)
ps:我用xxx
替换了公司类路径前缀。
答案 0 :(得分:2)
如果订阅者抛出异常,就会发生这种情况。
您可以在构建期间向com.google.common.eventbus.SubscriberExceptionHandler
提供EventBus
的实施,从而将默认记录器替换为您自己的记录器。
例如:
import com.google.common.eventbus.SubscriberExceptionContext;
import com.google.common.eventbus.SubscriberExceptionHandler;
class CustomExceptionHandler implements SubscriberExceptionHandler {
@Override
public void handleException(Throwable exception, SubscriberExceptionContext context) {
System.out.println("Handler '"+context.getSubscriber()+"' could not handle '"+context.getEvent().getClass().getName()+"'["+context.getEvent()+"]: " + exception.getMessage());
}
}
并像这样使用
EventBus eventBus = new EventBus(new CustomExceptionHandler());