【问题标题】:Error binding view to presenter?将视图绑定到演示者时出错?
【发布时间】:2012-05-03 10:46:22
【问题描述】:

运行 GWT 应用程序时出现此错误:

java.lang.AssertionError: This UIObject's element is not set; you may be missing a call to either Composite.initWidget() or UIObject.setElement()

public class MainView extends Composite implements HeaderPresenter.MyView {
  // Code omitted
}

在 Gin ClientModule.java configure() 函数中,我有以下代码:

bindPresenter(HeaderPresenter.class, HeaderPresenter.MyView.class,
                MainView.class, HeaderPresenter.MyProxy.class);

在视图类中,initWidget() 被正确调用并与小部件一起传递,什么可能导致错误?

【问题讨论】:

  • 我的应用程序曾经使用我在基于 UiBinder 视图的 bindPresenter 中使用的视图。但是由于某种原因,我需要使用从 Composite 扩展的普通 GWT 小部件的视图。现在不行了。
  • 你应该发布更多关于你的MainView类的代码。

标签: gwt dependency-injection guice gwt-gin gwt-platform


【解决方案1】:

UIObject.setElement 未被调用时会发生此错误。如果您使用非空小部件调用Composite.initWidget,请确保该小部件正确设置了自己的元素。如果这是一个标准的 GWT 小部件,它应该这样做,否则传递给 initWidget 的小部件可能没有正确设置。

【讨论】:

  • 我必须对插入到构造函数之外的 initWidget() 中的小部件进行编码,即 Horizo​​ntalPanel panel = new Horizo​​ntalPanel() 而不是在构造上对其进行初始化(尽管在传递给 initWidget 之前进行初始化当然)
  • 这就是问题所在 - 您在将复合材料添加到其父级之后添加根小部件,因此无法绘制任何内容。系统是这样工作的,此时必须调用 initWidget。一种可能的破解方法是使用面板初始化小部件,然后将 Horizo​​ntalPanel 添加到 那个 面板。
【解决方案2】:

这就是我创建Composite 的方式,稍后我将在视图中使用它。

public class MyCustomBox extends Composite {

    private static MyCustomBoxUiBinder uiBinder = GWT.create(MyCustomBoxUiBinder.class);

    interface MyCustomBoxUiBinder extends UiBinder<Widget, MyCustomBox> {
    }

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

【讨论】:

  • MyCustomBoxUiBinder 扩展了什么类?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多