1

GWT 2.5.0

一个使用 ListEditor 的简单案例在下面失败了,我错过了什么?

public class OneBean {

    private String name;

    public OneBean() {
    }

    public OneBean(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "OneBean [name=" + name + "]";
    }

}
public class OneListEditor extends Composite implements
        IsEditor<ListEditor<OneBean, OneEditor>> {

    interface OneListUiBinder extends UiBinder<Widget, OneListEditor> {}
    OneListUiBinder uiBinder = GWT.create(OneListUiBinder.class);

    @UiField
    VerticalPanel panel;

    public OneListEditor() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @Override
    public ListEditor<OneBean, OneEditor> asEditor() {
        return listEditor;
    }

    private ListEditor<OneBean, OneEditor> listEditor = ListEditor
            .of(new EditorSource<OneEditor>() {
                @Override
                public OneEditor create(int index) {
                    OneEditor widget = new OneEditor();
                    panel.insert(widget, index);
                    return widget;
                }
            });

}
public class OneEditor extends Composite implements Editor<OneBean> {

    interface OneUiBinder extends UiBinder<Widget, OneEditor> {}
    OneUiBinder uiBinder = GWT.create(OneUiBinder.class);

    @UiField
    TextBox name;

    public OneEditor() {
        initWidget(uiBinder.createAndBindUi(this));
    }

}
public class OneListEditorApp implements EntryPoint {

    @Override
    public void onModuleLoad() {
        List<OneBean> beans = new ArrayList<OneBean>();
        beans.add(new OneBean("1st bean"));
        beans.add(new OneBean("2nd bean"));

        OneListEditor oneListEditor = new OneListEditor();
        oneListEditor.asEditor().setValue(beans); // exception thrown here!

        RootPanel.get().add(oneListEditor);
    }

}
java.lang.NullPointerException: null
    at com.google.gwt.editor.client.adapters.ListEditorWrapper.attach(ListEditorWrapper.java:95)
    at com.google.gwt.editor.client.adapters.ListEditor.setValue(ListEditor.java:164)
    at OneListEditorApp.onModuleLoad ....
  void attach() {
    editors.addAll(editorSource.create(workingCopy.size(), 0));
    for (int i = 0, j = workingCopy.size(); i < j; i++) {
      chain.attach(workingCopy.get(i), editors.get(i)); // ListEditorWrapper NPE here!
    }
  }

@编辑

根据来自 的回答@Thomas Broyer,NPEEditDriver连接到OneListEditor下面之后就消失了,

interface OneEditorDriver extends
        SimpleBeanEditorDriver<OneBean, OneEditor> {}

OneEditorDriver driver = GWT.create(OneEditorDriver.class);

@Override
public ListEditor<OneBean, OneEditor> asEditor() {
    listEditor.setEditorChain(new EditorChain<OneBean, OneEditor>() {
        @Override
        public OneBean getValue(OneEditor subEditor) {
            return null;
        }
        @Override
        public void detach(OneEditor subEditor) {
        }
        @Override
        public void attach(OneBean object, OneEditor subEditor) {
            driver.initialize(subEditor);
            driver.edit(object);
        }
    });
    return listEditor;
}
4

1 回答 1

2

您没有使用 an EditorDriver,因此未使用 anListEditor初始化EditorChain,因此chainnullNPE。案例制作。

⇒ 使用EditorDriver(或不使用ListEditor

于 2012-12-14T15:14:38.763 回答