【问题标题】:GWT : failed to pick up html element by id as rootpanel returns nullGWT:无法通过 id 获取 html 元素,因为 rootpanel 返回 null
【发布时间】:2014-10-09 22:37:16
【问题描述】:

我是 GWT 的新手。我们正在使用 GWT 2.4.0、jdk 1.6、mvp4g 1.4.0。

我正在尝试构建一个 Web 应用程序,其中我必须在视图中嵌入一些 html 代码,然后必须在该 html 中从视图中获取特定的 id 以添加或删除元素或小部件。 我的 html 代码嵌入良好,但拉起元素不起作用。

这是我的 HTML 文件的样子:

<table align="center" border="1" cellpadding="1" cellspacing="10" id="transaction_monitor" style="width:100%;border: 1px solid black;padding: 5px;">
    <tbody>
        <tr id="tran_header" style="background-color:#CCFFFF;border:0px">
            <td colspan="2">&nbsp;</td>
        </tr>
        <tr id="tran_body">
            <td style="background-color:#CCFFFF;">
            <div id="clientContainer"></div>
            </td>
            <td style="background-color:#CCFFFF;">&nbsp;</td>
        </tr>
        <tr id="tran_footer" style="background-color:#CCFFFF;">
            <td colspan="2">&nbsp;</td>
        </tr>
    </tbody>
</table>

在视图中,我已通过以下方式嵌入此 html 代码:

    flexTableLayout = new FlexTable();

    initWidget( flexTableLayout );

    //Window.alert(Resources.INSTANCE.synchronous().getText());

    HTML htmlPanel = new HTML();
    String html = Resources.INSTANCE.synchronous().getText();
    htmlPanel.setHTML(html);

    flexTableLayout.setWidget( 0, 0, htmlPanel );

这部分工作正常并按预期创建视图。使用 F12 我可以在 browser-page-source 中找到 div id="clientContainer" 部分。

但在那之后,我试图通过 id "clientContainer" 从该 html 中获取 "div" 元素并将标签添加为:

Label lblFile = new Label( "File:" );
RootPanel.get("clientContainer").add(lblFile);

这部分没有按预期工作。在我测试的过程中,我发现“RootPanel.get("clientContainer")”正在返回 null。

我什至尝试过

Document.get().getElementById("clientContainer").add(lblFile);

而且 Document.get().getElementById("clientContainer") 也失败了。

我的 .gwt.xml 是:

  <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.6.4//EN" "http://google-web-toolkit.googlecode.com/svn/tags/1.6.4/distro-source/core/src/gwt-module.dtd">
<module rename-to='onlinetoolkit'>
  <!-- Inherit the core Web Toolkit stuff.                        -->
  <inherits name='com.google.gwt.user.User'/>

  <!-- Inherit the default GWT style sheet.  You can change       -->
  <!-- the theme of your GWT application by uncommenting          -->
  <!-- any one of the following lines.                            -->
  <inherits name='com.google.gwt.user.theme.standard.Standard'/>

  <!-- Other module inherits                                      -->
  <inherits name='com.mvp4g.Mvp4gModule'/>
  <inherits name="com.google.gwt.resources.Resources" />

  <!-- Specify the app entry point class.                         -->
  <entry-point class='com.mvp4g.client.Mvp4gEntryPoint'/>
</module>

谁能指出我缺少什么?

提前致谢。

【问题讨论】:

    标签: gwt mvp4g


    【解决方案1】:

    我发现我的 flexTableLayout 还没有附加到 rootpanel。现在查询这个还为时过早。

    【讨论】:

      猜你喜欢
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多