GWT JSNI javascript 到 Java 不起作用 [英] GWT JSNI javascript to Java not working

查看:26
本文介绍了GWT JSNI javascript 到 Java 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的 Javascript 代码中调用 Java 方法.这是针对使用 Phonegap 的 Windows Phone 7 应用.

I am trying to invoke a Java Method from my Javascript code. This is for a Windows Phone 7 app using Phonegap.

我的 javascript 代码中有以下内容.

I have the following in my javascript code.

document.addEventListener("backbutton", onBackKeyDown, false);
 function onBackKeyDown(){

    }

在我的 Java 代码中,我有以下内容.

And in my Java code I have the following.

  public static native void exportStaticMethod() /*-{
    $wnd.onBackKeyDown = 
        $entry(this.@com.mycompany.myapp.client.MyApp::hideSettingsWidgets());

    }-*/;

然后在 onModuleLoad() 我这样调用它:

Then in the on onModuleLoad() I am calling it like so:

MyApp.exportStaticMethod();

它不起作用我在 hideSettingsWidgets() 中有一个警报,但它永远不会显示.

It does not work I have an alert in the hideSettingsWidgets() but it never gets shown.

*编辑*这里还有一些代码.Javascript 中未添加 EventListener.它是在java代码中专门添加的.我最初无法让听众注册,所以这是我添加的内容.

*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();
}-*/;

在我的 JavaScript 中

And in my JavaScript

function removeTheListener(){
        document.removeEventListener("backbutton", onBackKeyDown, false);

        }

这是我对 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

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);
    }-*/;

所以我不确定我哪里出错了.我没有添加你提到的 ArrayList<> 因为不确定添加页面时事件侦听器没有运行.

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.

似乎 showSettingsWidgets() 永远不会运行

推荐答案

addEventListener 代码可能在页面加载时正在运行,对吗?这会将您的空函数 onBackKeyDown 映射到后退按钮事件.然后,当您的模块加载时,您尝试重新定义 onBackKeyDown 函数成为一个新函数 - 但旧函数已经附加到您尝试侦听的事件.

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(使用字符串而不是侦听器函数):

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!";

要解决此问题,您需要在其他问题中进行交叉操作,使用 GWT JSNI 将事件侦听器添加到文档,以及您在此处做什么.将 Java 函数包装在 $entry 调用中,并将其传递给 $doc.addEventListener 是最合乎逻辑的(尽管我对 WP7 了解不多).

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);
}-*/;

还有一件事 - 记住我们正在用原生代码编写 JavaScript,当 hideSettingsWidgets() 方法被调用时,this 会是什么?JavaScript 不知道所有 Java 实例方法都需要一个 this 来运行(并且 JavaScript 在 B 上运行对象 A 的方法没有问题 - A.method.call(B)code> 是完全合法的,而且通常很有帮助).我们需要确保 this 表示我们认为它的作用:

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);
}-*/;

哎呀,结果你的方法无论如何都是静态的,所以this实际上没有任何意义!要么将 exportStaticMethod/setupJavaHandler 更改为非静态并直接调用它(可能在您现在拥有的 onModuleLoad 中),或者传入一个调用 hideSettingsWidgets() 的实例,就像我们在前面的示例中对 app 所做的一样.

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();

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆