【问题标题】:Calling Java Method from GWT JSNI从 GWT JSNI 调用 Java 方法
【发布时间】:2011-09-06 20:25:49
【问题描述】:

首先,是的,我已经搜索并找到了这个答案:

GWT JSNI - problem passing Strings

我正在尝试从 JSNI 方法调用 java 方法,但没有到达任何地方。我已经尝试了上面给出的建议,但它仍然不起作用。

代码如下:

public native void initCustomListeners(MapmakerMapViewPresenter.MyView view) /*-{
//public native void initCustomListeners() /*-{

    $wnd.getLocationDescriptions = function(lng, lat) {
        $entry(view.@org.jason.mapmaker.client.view.MapmakerMapViewImpl::getLocationDescriptions(DD)(lng, lat));
    }

    $wnd.google.maps.event.addListener(map, 'click', function(event) {
        var lat = event.latLng.lat().toFixed(3);
        var lng = event.latLng.lng().toFixed(3);
        alert("(" + lat + ", " + lng + ")");
        $wnd.getLocationDescriptions(lng, lat);

        alert("Test!");
    });
}-*/;    @Override
public void getLocationDescriptions(double lng, double lat) {
    getUiHandlers().doGetLocationDescriptions(lng, lat);
}

谁能帮帮我?

杰森

【问题讨论】:

    标签: java gwt


    【解决方案1】:

    我不知道这是否是问题所在(您甚至没有说明该代码的行为方式与您期望它的行为方式),但您的代码中存在一些错误:

    • $entry 包装了一个函数,所以你必须调用它返回的函数,而不是(无用地)在调用后包装函数的结果! IE。 $entry(function(lat,lng) { foo.@bar.Baz::quux(DD)(a, b); } 而不是 $entry(foo.@bar.Baz::quux(DD)(a, b))

    • 您在 map 上使用 addListener,但从未定义过该变量。

    【讨论】:

    • 我仍然缺少一些东西...你能看到我最近的帖子吗?
    • 我认为我建议使用$entry() 的方式有误。查看更新的答案。
    • 它试图调用该方法,但抛出一个 ClassCastException。 alert() 显示我正在按要求传递一个 double,但找出试图强制转换的 Class 是非常重要的。
    【解决方案2】:

    我仍然遗漏了一些东西,而且我已经盯着给定的代码看了很长时间。

    这是当前版本的代码:

    public native void initCustomListeners(JavaScriptObject map) /*-{
    
        var that = this;
    
        $wnd.getLocationDescriptions = function(lng, lat) {
            $entry(that.@org.jason.mapmaker.client.presenter.MapmakerMapViewPresenter::doGetLocationDescriptions(DD))(lng,lat);
        }
    
        $wnd.google.maps.event.addListener(map, 'click', function(event) {
            var lat = event.latLng.lat();
            var lng = event.latLng.lng();
            alert("(" + lat + ", " + lng + ")");
            @com.google.gwt.core.client.GWT::log(Ljava/lang/String;)("calling $wnd.getLocationDescriptions()");
            $wnd.getLocationDescriptions(lng, lat);
            @com.google.gwt.core.client.GWT::log(Ljava/lang/String;)("called $wnd.getLocationDescriptions()");
        });
    }-*/;
    

    调用导致 ClassCastException。抱歉,如果我遗漏了一些应该很明显的内容。

    【讨论】:

      【解决方案3】:

      我看到的一个错误是你应该这样做:

      $wnd.getLocationDescriptions = $entry(@org.jason.mapmaker.client.view.MapmakerMapViewImpl::getLocationDescriptions(DD)(lng, lat));
      

      (不使用您使用的函数'wrapper'),然后通过以下方式从javascript调用该函数:

      $wnd.getLocationDescriptions(lng, lat);
      

      另外,“那个”变量不是必需的(或“这个”),在 @ 之前。我也不确定 $wnd.google.maps.event.addListener( 东西,你确定在 $wnd 上分配了这样一个对象吗?

      最后,再看看这个:

      https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多