我正试图从我的JSNI函数中调用一个java方法,但由于某种原因,它永远不会工作。我到底做错了什么?
以下是我的代码
/**
For UI button click method...
*/
private native void test(String param)
/*-{
var a=(function b(p)
{
this.@com.(...).TestClass::setTest(Ljava/lang/String;)(p);
})(param);
}-*/
private void setTest(String param){Window.alert(param);}感谢所有有用的评论。
发布于 2012-05-02 10:21:29
您需要在function块之外引用this:
/**
* For UI button click method...
*/
private native void test(String param) /*-{
var theInstance = this;
var a = ( function b(p) {
theInstance.@com.(...).TestClass::setTest(Ljava/lang/String;)(p);
})(param);
}-*/;
private void setTest(String param){
Window.alert(param);
}发布于 2011-07-12 13:06:42
您使用this关键字可能会导致此问题。在您的上下文中,this关键字指向闭包
(function b(p)
{
this.@com.(...).TestClass::setTest(Ljava/lang/String;)(p);
})(param);理想情况下,它应该指向从中编译GWT的函数
private native void test(String param)这句话。
尝试使用此代码段(我不确定语法是否正确,请使用GWT JSNI wiki进行验证):
private native void test(String param)
/*-{
var a = this.@com.(...).TestClass::setTest(Ljava/lang/String;)(param);
}-*/顺便说一句,一个函数的唯一目的是调用另一个函数,这就是代码气味。
发布于 2011-07-12 15:16:11
是的,正如Zasz所指出的那样,你的代码过于复杂了(如果你真的想要提供一个JavaScript方法,但在这种情况下,你必须以一种完全不同的方式来做……)
所以我测试了代码,这是可行的:
/* JNI Example method... */
private native void test(String param) /*-{
this.@com.stefank.client._53_JavaScriptOverlayTypes::setTest(Ljava/lang/String;)(param);
}-*/;
private void setTest(String param){
Window.alert(param);
} https://stackoverflow.com/questions/6658800
复制相似问题