1

我有以下GWT代码:

package my.package.name;

public class MyImpl {

    public void scan(AsyncCallback<String> callBack) {
        this.callBack = callBack;
        scanJNSI();
    }

    private native void scanJNSI()/*-{
        var instance = this;
        instance.@my.package.name.MyImpl::onError(Ljava/lang/String;)("BROWSER");
    }-*/;

    private void onError(String obj) {
        callBack.onFailure(new Exception(obj));
    }
}

当我打电话时:

instance.scan(new AsyncCallback<String>() {
    @Override
    public void onSuccess(String result) {
        Window.alert(result);
    }

    @Override
    public void onFailure(Throwable caught) {
        Window.alert(caught.getMessage());
    }
});

我得到一个Caused by: java.lang.ClassCastException: null

Caused by: java.lang.ClassCastException: null
    at java.lang.Class.cast(Class.java:2990)
    at com.google.gwt.dev.shell.JsValueGlue.get(JsValueGlue.java:163)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:57)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:337)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:218)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid(ModuleSpace.java:289)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:107)
    at my.package.name.MyImpl.scanJNSI(MyImpl.java)
    at my.package.name.MyImpl.scan(MyImpl.java:12)

什么可能是它无法投射的类?

4

1 回答 1

0

我找到了解决方法:

我更换了:

instance.@my.package.name.MyImpl::onError(Ljava/lang/String;)("BROWSER");

和:

instance.@my.package.name.MyImpl::onError(Lcom/google/gwt/core/client/JavaScriptObject;)({name:"BROWSER"});

private void onError(String obj) {
    callBack.onFailure(new Exception(obj));
}

和:

private void onError(JavaScriptObject obj) {
    JSOModel jso = obj.cast();
    callBack.onFailure(new Exception(jso.get("name")));
}

它奏效了..

我不知道使用String类是否有问题。

于 2012-11-29T00:15:39.177 回答