繁体   English   中英

从GWT JSNI调用Java方法

[英]Calling Java Method from GWT JSNI

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

GWT JSNI - 传递字符串的问题

我试图从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);
}

谁能帮我吗?

贾森

我不知道这是不是问题(你甚至没有说明代码的行为与预期的行为有关)但你的代码中有一些错误:

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

  • 你在map上添加了addListener ,但从未定义过该变量。

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

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

(不使用你使用的函数'wrapper'),然后通过javascript调用函数:

$wnd.getLocationDescriptions(lng, lat);

此外,在@之前,'that'变量不是必需的(或'this')。 我也不确定$ wnd.google.maps.event.addListener(事情,你确定在$ wnd上分配了这样的对象吗?

最后,再看一遍:

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

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

这是代码的当前版本:

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。 对不起,如果我错过了一些显而易见的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM