【问题标题】:How to call method of @UiField field in GWT如何在 GWT 中调用 @UiField 字段的方法
【发布时间】:2014-11-19 11:12:32
【问题描述】:

这是我的课

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 在这种情况下是如何工作的。请帮我解决它并设置数据。

【问题讨论】:

    标签: gwt uibinder


    【解决方案1】:

    只有当您在 UiBinder 实例上调用 initAndBindUi 时,@UiFields 才会被“注入”,并以 @UiFields 作为参数传递一个实例。

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

    【讨论】:

      猜你喜欢
      • 2013-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-22
      相关资源
      最近更新 更多