GWT JSNI JavaScript到Java不起作用 [英] GWT JSNI javascript to Java not working
问题描述
我试图从我的Javascript代码中调用Java方法。这是用于使用Phonegap的Windows Phone 7应用程序。
我的javascript代码中包含以下内容:
document.addEventListener(backbutton,onBackKeyDown,false);
函数onBackKeyDown(){
}
在我的Java代码我有以下几点。
public static native void exportStaticMethod()/ * - {
$ wnd.onBackKeyDown =
$ entry(this。@ com.mycompany.myapp.client.MyApp :: hideSettingsWidgets());
} - * /;
然后在onModuleLoad()中,我这样调用它:
MyApp.exportStaticMethod();
它不起作用我在 hideSettingsWidgets() code>但它永远不会显示。
* 编辑 *
这里有一些更多的代码。 EventListener不会在Javascript中添加。它专门添加了Java代码。
public static native void removeBackListener()/ * - {
$ wnd.removeTheListener();
} - * /;
在我的JavaScript中
function removeTheListener(){
document.removeEventListener(backbutton,onBackKeyDown,false);
$ b
这是我对的调用hideSettingsWidgets ()
public void hideSettingsWidgets(){
for(int i = 0; i< settingsScreenWidgets.length; i ++){
settingsScreenWidgets [i] .setVisible(false);
}
alertString(Working);
removeBackListener();
}
我打电话你给我的方法 showSettingsWidgets()
p
rivate void showSettingsWidgets(){
for(int i = 0; i< settingsScreenWidgets.length; i ++){
settingsScreenWidgets [i] .setVisible(true );
}
setCurrentImage();
setOnOffImage();
setupJavaHandler();
它好像添加了EventListener
public native void setupJavaHandler()/ * - {
var app = this;
var onBackKeyDown = $ entry(function(){
app。@ com.mycompany.myapp.client.MyApp :: hideSettingsWidgets();
});
$ doc.addEventListener(backbutton,onBackKeyDown,false);
} - * /;
所以我不确定自己出错的地方。我没有添加你提到的ArrayList<>,因为当页面被加载时不确定和Event Listener没有运行。
看起来像 showSettingsWidgets()
永远不会运行
onBackKeyDown
映射到backbutton事件。然后,当您的模块加载时,您尝试重新定义 onBackKeyDown
函数为新函数 - 但旧函数已附加到事件你试图听。
这大致相当于(用字符串代替侦听器函数):
//首先,让事物保持'侦听器',并定义第一个
List< String> strings = new ArrayList< String>();
String onBackKeyDown =abcd;
strings.add(onBackKeyDown);
//然后,重新定义字符串,但不要更改列表!
onBackKeyDown =zyxw;
断言strings.contains(onBackKeyDown):哎呦,重新分配,但没有添加!;
为了解决这个问题,你需要在你的另一个问题中做什么添加Eventlisteners与GWT JSNI文件,以及你在这里做什么。将Java函数包装在 $ entry
调用中,并将它传递给 $ doc.addEventListener
,这是最合理的虽然我对WP7了解不多)。
public static native void setupJavaHandler()/ * - {
var onBackKeyDown = $ entry(this。@ com.mycompany.myapp.client.MyApp :: hideSettingsWidgets());
$ doc.addEventListener(backbutton,onBackKeyDown,false);
} - * /;
还有一件事 - 记住我们正在使用本地代码编写JavaScript,会发生什么 this
何时调用 hideSettingsWidgets()
方法? JavaScript并不知道所有的Java实例方法都需要这个
来运行(并且JavaScript在B上运行对象A的方法没有问题 - A .method.call(B)
是完全合法的,并且通常很有帮助)。我们需要确保这个
表示我们认为它的作用:
public static native setupJavaHandler()/ * - {
var app = this;
var onBackKeyDown = $ entry(function(){
app。@ com.mycompany.myapp.client.MyApp :: hideSettingsWidgets();
});
$ doc.addEventListener(backbutton,onBackKeyDown,false);
} - * /;
编辑:糟糕,原来你的方法是静态的,所以 this
实际上并不意味着什么!将 exportStaticMethod
/ setupJavaHandler
更改为非静态并直接调用它(可能在 onModuleLoad ,或者传入一个实例来调用
hideSettingsWidgets()
,就像我们用
public native void setupJavaHandler()/ * - {
var app = this;
var onBackKeyDown = $ entry(function(){
app。@ com.mycompany.myapp.client.MyApp :: hideSettingsWidgets();
});
$ doc.addEventListener(backbutton,onBackKeyDown,false);
} - * /;
//在onModuleLoad中:
setupJavaHandler();
或
public static native void setupJavaHandler(MpApp app)/ * - {
// var app = this;
var onBackKeyDown = $ entry(function(){
app。@ com.mycompany.myapp.client.MyApp :: hideSettingsWidgets();
});
$ doc.addEventListener(backbutton,onBackKeyDown,false);
} - * /;
//在onModuleLoad中:
MyApp.setupJavaHandler(this);
I am trying to invoke a Java Method from my Javascript code. This is for a Windows Phone 7 app using Phonegap.
I have the following in my javascript code.
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(){
}
And in my Java code I have the following.
public static native void exportStaticMethod() /*-{
$wnd.onBackKeyDown =
$entry(this.@com.mycompany.myapp.client.MyApp::hideSettingsWidgets());
}-*/;
Then in the on onModuleLoad() I am calling it like so:
MyApp.exportStaticMethod();
It does not work I have an alert in the hideSettingsWidgets()
but it never gets shown.
*EDIT* Here is some more code. The EventListener is not added in the Javascript. It is specifically added withing the java code. I could not get the listeners to register originally so here is what I added.
public static native void removeBackListener() /*-{
$wnd.removeTheListener();
}-*/;
And in my JavaScript
function removeTheListener(){
document.removeEventListener("backbutton", onBackKeyDown, false);
}
Here is my call to hideSettingsWidgets()
public void hideSettingsWidgets(){
for(int i=0;i<settingsScreenWidgets.length;i++){
settingsScreenWidgets[i].setVisible(false);
}
alertString("Working");
removeBackListener();
}
And I am calling the method you gave me inside showSettingsWidgets()
p
rivate void showSettingsWidgets(){
for(int i=0;i<settingsScreenWidgets.length;i++){
settingsScreenWidgets[i].setVisible(true);
}
setCurrentImage();
setOnOffImage();
setupJavaHandler();
}
It does seem to be adding the EventListener that is inside your
public native void setupJavaHandler() /*-{
var app = this;
var onBackKeyDown = $entry(function() {
app.@com.mycompany.myapp.client.MyApp::hideSettingsWidgets();
});
$doc.addEventListener("backbutton", onBackKeyDown, false);
}-*/;
So I am not sure where I am going wrong. I did not add the ArrayList<> you mentioned because was not sure to and the Event Listener was not running when page was Loaded.
Seems like showSettingsWidgets()
never gets run
The addEventListener
code is probably running when the page loads, right? This will map your empty function onBackKeyDown
to the backbutton event. Then, when your module loads, you attempt to redefine the onBackKeyDown
function to be a new one - but the old one was already attached to the event you are trying to listen to.
This is roughly the equivalent of this (with strings instead of listener functions):
// first, make the thing to hold the 'listener', and define the first one
List<String> strings = new ArrayList<String>();
String onBackKeyDown = "abcd";
strings.add(onBackKeyDown);
// then, redefine the string, but don't change the list!
onBackKeyDown = "zyxw";
assert strings.contains(onBackKeyDown) : "Whoops, reassigned, but not added!";
To fix this, you need a cross between what you are doing in your other question, Adding Eventlisteners to document with GWT JSNI, and what you are doing here. Wrapping the Java function in an $entry
call, and passing that to $doc.addEventListener
makes the most logical sense (though I don't know a lot about WP7).
public static native void setupJavaHandler() /*-{
var onBackKeyDown = $entry(this.@com.mycompany.myapp.client.MyApp::hideSettingsWidgets());
$doc.addEventListener("backbutton", onBackKeyDown, false);
}-*/;
One more thing - remembering that we are writing JavaScript in that native code, what is going to be this
when that hideSettingsWidgets()
method is called? JavaScript doesn't know that all Java instance methods need a this
to run on (and JavaScript has no problem running methods for object A on B - A.method.call(B)
is totally legal, and often helpful). We need to be sure that this
means what we think it does:
public static native void setupJavaHandler() /*-{
var app = this;
var onBackKeyDown = $entry(function() {
app.@com.mycompany.myapp.client.MyApp::hideSettingsWidgets();
});
$doc.addEventListener("backbutton", onBackKeyDown, false);
}-*/;
Edit: Oops, turns out your method was static anyway, so this
doesn't actually mean anything! Either change exportStaticMethod
/setupJavaHandler
to be non static and call it directly (probably in your onModuleLoad
as you have it now), or pass in an instance to call hideSettingsWidgets()
on, like we are doing with app
in the previous sample.
public native void setupJavaHandler() /*-{
var app = this;
var onBackKeyDown = $entry(function() {
app.@com.mycompany.myapp.client.MyApp::hideSettingsWidgets();
});
$doc.addEventListener("backbutton", onBackKeyDown, false);
}-*/;
// in onModuleLoad:
setupJavaHandler();
or
public static native void setupJavaHandler(MpApp app) /*-{
//var app = this;
var onBackKeyDown = $entry(function() {
app.@com.mycompany.myapp.client.MyApp::hideSettingsWidgets();
});
$doc.addEventListener("backbutton", onBackKeyDown, false);
}-*/;
// in onModuleLoad:
MyApp.setupJavaHandler(this);
这篇关于GWT JSNI JavaScript到Java不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!