使用StackLayoutPanel和StackPanel时出现java IllegalStateException

时间:2014-06-30 10:33:36

标签: java gwt

我正在使用此代码

测试GWT StackLayoutPanel类
public class MenuView extends StackLayoutPanel {

    interface Binder extends UiBinder<StackLayoutPanel, MenuView> {
    }

    private static final Binder binder = GWT.create(Binder.class);

    @UiField Users users;

    @UiField Configurations configurations;

    public MenuView(Unit unit) {
        super(unit);
        initWidget(binder.createAndBindUi(this));
    }

    @Override
    public int getVisibleIndex() {
        return super.getVisibleIndex();
    }

    @Override
    public Widget getVisibleWidget() {
        return super.getVisibleWidget();
    }
}


————————————————————————

public class Users extends Composite {

    /**
     * Callback when user items are selected.
     */
    public interface Listener {
        void onUserSelected(User user);
    }

    /**
     * Sets the listener that will be notified when an item is selected.
     */
    public void setListener(Listener listener) {
        this.listener = listener;
    }

    @SuppressWarnings("unused")
    private Listener listener;

    interface Binder extends UiBinder<Widget, Users> {
    }

    interface Style extends CssResource {
        String item();
    }

    private static final Binder binder = GWT.create(Binder.class);

    @UiField
    ComplexPanel panel;
    @UiField
    Style style;

    public Users() {
        initWidget(binder.createAndBindUi(this));
    }
}

MenuView.ui.xml File
<ui:UiBinder
  <g:StackLayoutPanel styleName='{style.menuview}' unit='EM'>
    <g:stack>
      <g:header size='3'><div class='{style.stackHeader}'><div class='{style.usersIcon}'/> Users</div></g:header>
      <sensport:Users ui:field='users'/>
    </g:stack>
  </g:StackLayoutPanel>
</ui:UiBinder>

user.ui.xml File
<ui:UiBinder>
  <g:FlowPanel styleName='{style.users}' ui:field='panel'/>
</ui:UiBinder>

当我运行测试时,我获得了一个java IllegalStateException。您是否知道此错误?

java.lang.IllegalStateException:Composite.initWidget()只能被调用一次。     在com.google.gwt.user.client.ui.Composite.initWidget(Composite.java:127)     在com.google.gwt.user.client.ui.ResizeComposite.initWidget(ResizeComposite.java:30)     在be.lilab.sensport.client.MenuView。&lt; init&gt;(MenuView.java:26)     at be.lilab.sensport.client.Sensport_BinderImpl $ Widgets.build_menuView(Sensport_BinderImpl.java:122)     at be.lilab.sensport.client.Sensport_BinderImpl $ Widgets.get_menuView(Sensport_BinderImpl.java:118)     at be.lilab.sensport.client.Sensport_BinderImpl $ Widgets.build_f_SplitLayoutPanel2(Sensport_BinderImpl.java:106)     at be.lilab.sensport.client.Sensport_BinderImpl $ Widgets.get_f_SplitLayoutPanel2(Sensport_BinderImpl.java:100)     at be.lilab.sensport.client.Sensport_BinderImpl $ Widgets.build_f_DockLayoutPanel1(Sensport_BinderImpl.java:73)     at be.lilab.sensport.client.Sensport_BinderImpl $ Widgets.get_f_DockLayoutPanel1(Sensport_BinderImpl.java:66)     at be.lilab.sensport.client.Sensport_BinderImpl $ Widgets.access $ 0(Sensport_BinderImpl.java:65)     在be.lilab.sensport.client.Sensport_BinderImpl.createAndBindUi(Sensport_BinderImpl.java:15)     在be.lilab.sensport.client.Sensport_BinderImpl.createAndBindUi(Sensport_BinderImpl.java:1)     在be.lilab.sensport.client.Sensport.onModuleLoad(Sensport.java:47)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:606)     在com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:411)     在com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)     在com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:526)     在com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)     在java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

您正在扩展StackLayoutpanel,它扩展了ResizeComposite。 在StackLayoutPanel的构造函数中,调用initWidget方法。

所以,再次使用:

调用它
public MenuView(Unit unit) {
    super(unit);
    initWidget(binder.createAndBindUi(this));
}

会导致异常。在您的情况下,可能的解决方案是扩展Composite。

    public class MenuView extends Composite {

    interface Binder extends UiBinder<StackLayoutPanel, MenuView> {
    }

    private static final Binder binder = GWT.create(Binder.class);

    @UiField Users users;

    @UiField Configurations configurations;

    @UiField StackLayoutPanel panel;

    public MenuView(Unit unit) {
        super(unit);
        initWidget(binder.createAndBindUi(this));
    }

    @Override
    public int getVisibleIndex() {
        return panel.getVisibleIndex();
    }

    @Override
    public Widget getVisibleWidget() {
        return panel.getVisibleWidget();
    }
}

更新您的MenuViewui.xml:

<ui:UiBinder
  <g:StackLayoutPanel ui:field='panel' styleName='{style.menuview}' unit='EM'>
    <g:stack>
      <g:header size='3'><div class='{style.stackHeader}'><div class='{style.usersIcon}'/> Users</div></g:header>
      <sensport:Users ui:field='users'/>
    </g:stack>
  </g:StackLayoutPanel>
</ui:UiBinder>