我尝试取消注册时,在我的一个类中遇到以下错误。
java.lang.IllegalArgumentException:缺少事件处理程序 注释方法。是 [DerivedClass]注册?“ 在com.google.common.eventbus.EventBus.unregister(EventBus.java:227)
调用unregister(this)
的类具有公共@Subscribe
注释方法。
答案 0 :(得分:16)
问题在于错误消息误导我认为注释有问题。实际上,由于一些意想不到的控制流,该课程已经两次取消注册。
答案 1 :(得分:3)
源代码说明:
/**
* Unregisters all handler methods on a registered {@code object}.
*
* @param object object whose handler methods should be unregistered.
* @throws IllegalArgumentException if the object was not previously registered.
*/
public void unregister(Object object) {
Multimap<Class<?>, EventHandler> methodsInListener = finder.findAllHandlers(object);
for (Entry<Class<?>, Collection<EventHandler>> entry : methodsInListener.asMap().entrySet()) {
Class<?> eventType = entry.getKey();
Collection<EventHandler> eventMethodsInListener = entry.getValue();
handlersByTypeLock.writeLock().lock();
try {
Set<EventHandler> currentHandlers = handlersByType.get(eventType);
if (!currentHandlers.containsAll(eventMethodsInListener)) {
throw new IllegalArgumentException(
"missing event handler for an annotated method. Is " + object + " registered?");
}
currentHandlers.removeAll(eventMethodsInListener);
} finally {
handlersByTypeLock.writeLock().unlock();
}
}
}