如何在GWT中调用@UiField字段的方法

时间:2014-11-19 11:12:32

标签: gwt uibinder

这是我的课程

public class PersonView extends Composite{

    private Person person;

    @UiField
    FirstnameView firstnameView;
    @UiField
    LastnameView lastnameView;

    public PersonView(Person person) {
        setData(person);
    }

    public void setData(Person person) {
        firstnameView.setData(person.getFirstname());
        lastnameView.setData(person.getLastname());
    }
}
public class FirstnameView extends Composite{

        private Firstname firstname;

        @UiField
        TextBox firstnameTextBox;

        public FirstnameView() {
        }

        public void setData(Firstname firstname) {
            firstnameTextBox.setValue(firstname.getFirstname());
        }
    }
public class LastnameView extends Composite{

        private Lastname lastname;

        @UiField
        TextBox lastnameTextBox;

        public LastnameView() {
        }

        public void setData(Lastname lastname) {
            lastnameTextBox.setValue(lastname.getLastname());
        }
    }

我的问题是,当我调用方法setData(person)时,我收到以下错误

java.lang.NullPointerException: null in the line `firstnameView.setData(person.getFirstname())`

我认为@UiField FirstnameView firstnameView(以及lastnameView)也不会创建对象。我真的不明白gwt在这种情况下是如何工作的。请帮我解决并设置数据。

1 个答案:

答案 0 :(得分:1)

当您在@UiField实例上调用initAndBindUi并传递一个以UiBinder为参数的实例时,

@UiField仅被“注入”。

换句话说,您的PersonView构造函数应该首先初始化UiBinder个实例并调用binder.initAndBindUi(this),然后才能调用setData()