我正在尝试运行GWT Elemental示例(来自GWT 2.5.1存储库),它在运行时给我一个异常。我不得不对代码进行一些更改才能使其运行,我猜这是对Elemental的更改的结果,这些更改未在示例代码中反映出来。这是代码,注意到我的更改:
@Override
public void onModuleLoad() {
final ButtonElement btn = getDocument().createButtonElement();
btn.setInnerHTML("w00t?");
btn.getStyle().setColor("red");
getDocument().getBody().appendChild(btn);
final DivElement div = getDocument().createDivElement();
getDocument().getBody().appendChild(div);
EventListener listener = new EventListener() {
@Override
public void handleEvent(Event evt) {
final XMLHttpRequest xhr = getWindow().newXMLHttpRequest();
xhr.setOnload(new EventListener() {
@Override
public void handleEvent(Event evt2) {
div.setInnerHTML(xhr.getResponseText());
}
});
xhr.open("GET", "/snippet.html");
xhr.send();
getWindow().setTimeout(new TimeoutHandler() { // *** changed from "new Window.TimerCallback()"
@Override
public void onTimeoutHandler() { // *** changed from "public void fire()"
getWindow().alert("timeout fired");
}
}, 1000);
btn.removeEventListener(Event.CLICK, this, false);
}
};
btn.addEventListener(Event.CLICK, listener, false);
}
原始代码在这里:ElementalExample.java。
异常发生在行btn.addEventListener(Event.CLICK, listener, false);
上,似乎是由其中一个JavaScript库中的空对象引起的。如果我注释掉btn.addEventListener()
调用,那么页面将显示在浏览器中,带有按钮(虽然它不起作用,当然没有事件监听器)。有没有其他人试图运行这个例子?如果是这样,你有没有在最新版本的GWT中运行的指针?
这是堆栈跟踪:
com.google.gwt.core.client.JavaScriptException: (String) : Invoking an instance method on a null instance
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.google.gwt.dev.shell.ModuleSpace.createJavaScriptException(ModuleSpace.java:80)
at com.google.gwt.dev.shell.ModuleSpace.createJavaScriptException(ModuleSpace.java:64)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:60)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
at elemental.js.dom.JsElementalMixinBase$.addEventListener$(JsElementalMixinBase.java)
at com.google.gwt.core.client.JavaScriptObject$.elemental_html_ButtonElement_addEventListener(JavaScriptObject.java)
at org.greatlogic.elementalfile.client.ElementalFileEntryPoint.onModuleLoad(ElementalFileEntryPoint.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:406)
at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:526)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Unknown Source)
(请注意,我已将代码粘贴到我自己的项目中,因此org.greatlogic.elementalfile.client.ElementalFileEntryPoint
类名称。)