EventHandler.java:
public abstract class EventHandler<E extends EventArgs> {
public abstract void HandleEvent(Object sender, E e);
}
Observers.java:
public class Observers<E extends EventArgs> {
private CopyOnWriteArrayList<EventHandler<? extends E>> mListeners = new CopyOnWriteArrayList<EventHandler<? extends E>>();
public void dispatchEvent(Object sender, E args) {
if (mListeners != null) {
for (EventHandler<? extends E> listener : mListeners) {
listener.HandleEvent(sender, args);
}
}
}
}
The following line:
listener.HandleEvent(sender, args);
Causes:
The method HandleEvent(Object, capture#3-of ? extends E) in the type
EventHandler is not applicable for the
arguments (Object, E)
Does anybody how to fix this?
EDIT1
The reason ? super E doesn’t work for me is that I have the following method inside Observers class:
public void addListener(EventHandler<? super E> listener) {
mListeners.add(listener);
}
And that causes:
The method add(EventHandler) in the type
CopyOnWriteArrayList> is not applicable for the
arguments (EventHandler)
EDIT2
The reason the change from ? super E to E doesn’t work for me because of this:
X is not applicable for the arguments Y, when X extends Y
It was already like that but that didn’t work neither 🙁
Why are you declaring
mListenersto be ~? extends Erather than justE?If you use
It’ll work.
Or adopt PECS (Producer Extends, Consumer Super). As others have suggested
with related changes to the
forloop.Edit: A fuller example. This shows no warnings or errors, and, based on what you’ve given, would work.