0

我一直在尝试写联系人。使用 gwt-platform 的示例我已经使用手动显示成功获得了联系人视图以获取联系人列表,我正在尝试使用 UiHandlers 添加按钮,但它没有在我的 ContactsPresenter 的 onAddButtonClicked() 中调用该方法。

休闲是我的观点的代码片段

public class ContactsView extends ViewWithUiHandlers<ContactsViewUiHandlers> implements  ContactsPresenter.MyView {

private final Widget widget;

@UiField
Button addButton;

@UiHandler("addButton")
public void onAddButtonClick(ClickEvent e){
    System.out.println("add button clicked");
    getUiHandlers().onAddButtonClicked();
}  

}

闲置的是 UiHandlers 接口

public interface ContactsViewUiHandlers extends UiHandlers{
        void onAddButtonClicked();
}

在我的演示者中,我正在使用 onAddButtonClicked() 中的事件总线触发一个事件。

但是控件没有提供给我的演示者,并且当我单击添加按钮时出现以下异常。

[ERROR] [contactsgwtp] - Uncaught exception escaped

10:17:10.527 [ERROR] [contactsgwtp] Uncaught exception escaped

com.google.gwt.event.shared.UmbrellaException: One or more exceptions caught, see full set in UmbrellaException#getCauses
    at com.google.gwt.event.shared.HandlerManager.fireEvent(HandlerManager.java:129)
    at com.google.gwt.user.client.ui.Widget.fireEvent(Widget.java:129)
    at com.google.gwt.event.dom.client.DomEvent.fireNativeEvent(DomEvent.java:116)
    at com.google.gwt.user.client.ui.Widget.onBrowserEvent(Widget.java:177)
    at com.google.gwt.user.client.DOM.dispatchEventImpl(DOM.java:1351)
    at com.google.gwt.user.client.DOM.dispatchEvent(DOM.java:1307)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:337)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:218)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:213)
    at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:292)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:546)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.NullPointerException: null
    at com.saik.in.client.core.ContactsView.onAddButtonClick(ContactsView.java:51)
    at com.saik.in.client.core.ContactsView_BinderImpl$1.onClick(ContactsView_BinderImpl.java:50)
    at com.google.gwt.event.dom.client.ClickEvent.dispatch(ClickEvent.java:54)
    at com.google.gwt.event.dom.client.ClickEvent.dispatch(ClickEvent.java:1)
    at com.google.gwt.event.shared.GwtEvent.dispatch(GwtEvent.java:1)
    at com.google.web.bindery.event.shared.EventBus.dispatchEvent(EventBus.java:40)
    at com.google.web.bindery.event.shared.SimpleEventBus.doFire(SimpleEventBus.java:193)
    at com.google.web.bindery.event.shared.SimpleEventBus.fireEvent(SimpleEventBus.java:88)
    at com.google.gwt.event.shared.HandlerManager.fireEvent(HandlerManager.java:127)
    at com.google.gwt.user.client.ui.Widget.fireEvent(Widget.java:129)
    at com.google.gwt.event.dom.client.DomEvent.fireNativeEvent(DomEvent.java:116)
    at com.google.gwt.user.client.ui.Widget.onBrowserEvent(Widget.java:177)
    at com.google.gwt.user.client.DOM.dispatchEventImpl(DOM.java:1351)
    at com.google.gwt.user.client.DOM.dispatchEvent(DOM.java:1307)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:337)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:218)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:213)
    at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:292)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:546)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363)
    at java.lang.Thread.run(Thread.java:662)

关于我所缺少的任何建议,在此先感谢

4

2 回答 2

3

显然getUiHandlers()null......但你知道:-)

I would expect the cause of this could be that you have forgotten to set the UI handlers when injecting in the associated presenter (which would be the normal procedure in GWTP) - something like:

public class ContactsPresenter extends Presenter<ContactsPresenter.MyView, ContactsPresenter.MyProxy> 
                               implements ContactsViewUiHandlers 
{
    public interface MyView extends View, HasUiHandlers { /* nothing */ }

    private PlaceManager placeManager;

    @Inject
    ContactsPresenter( final EventBus eventBus , final MyView view , 
                       final MyProxy proxy , final PlaceManager placeManager )
    {
        super( eventBus , view , proxy );
        this.placeManager = placeManager;
        getView().setUiHandlers( this );
    }
    ...

    @Override
    public void onAddButtonClicked()
    {
        placeManager.revealPlace( new PlaceRequest( "somewhereovertheocean" ) );
    }
    ...
}

(The operative part being getView().setUiHandlers( this ); and implements ContactsViewUiHandlers).

Incidentally, it is good practice to always check getUiHandlers() for null-ness before invoking any methods on it...

Hope that helps.

Cheers,

于 2012-11-01T09:45:58.367 回答
1

Looking at the exception you've provided I think you have missed one of the following points :

  1. ContactsPresenter should implement ContactsViewUiHandlers like this: ContactsPresenter implemets ContactsViewUiHandlers
  2. Set ContactsPresenter's reference as contactsView.UIHandlers like this: getView().setUiHandlers(this);

After doing both the things you should not get the mentioned Exception. I hope this helps you.

于 2012-11-01T09:54:29.953 回答