从JSNI调用Java方法

时间:2013-11-11 20:02:11

标签: java javascript gwt jsni

我想从JSNI变量test()调用Java方法successHandler()。但是,我收到错误

[ERROR] - Line 110: Missing qualifier on instance method 'com.gw.myProject.client.presenter.payments.PaymentProgramPresenter.test'

原始代码:

public static native void purchase(String token) /*-{

      var successHandler = function(status){ // Success handler
        return @com.gw.myProject.client.presenter.payments.PaymentProgramPresenter::test()();
      } 
      var failureHandler = function(status){ // Failure handler
        // $wnd.alert('testing');
      }

      $doc.purchaseAction(token, successHandler, failureHandler);
    }-*/;

    public void test() {
        this.onHide();
    }

1 个答案:

答案 0 :(得分:5)

您的test()不是静态的。因此,您需要将其设置为静态或指定实例或使购买非静态。

(此错误是“Cannot make a static reference to the non-static method methodName() from the type TypeName”的GWT版本)

public native void purchase(String token) /*-{

  var instance = this;

  var successHandler = function(status){ // Success handler
    return instance.@com.gw.myProject.client.presenter.payments.PaymentProgramPresenter::test()();
  } 
  var failureHandler = function(status){ // Failure handler
    // $wnd.alert('testing');
  }

  $doc.purchaseAction(token, successHandler, failureHandler);
}-*/;

public void test() {
    this.onHide();
}

您可以在gwtproject.org

找到真正优秀的教程

还有一个提示。如果你在JSNI中创建javascript回调,请用en $ entry() - function:

包装它们
$doc.purchaseAction(token,$entry( successHandler ), $entry( failureHandler));

这将启用GWT uncaughtExceptionHandler