调用JSNI方法时ClassCastException

时间:2012-11-28 23:57:39

标签: gwt classcastexception jsni

我有以下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)

它可能无法演出的课程是什么?

1 个答案:

答案 0 :(得分: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类是否是问题。