从GWT JSNI调用Java方法

4

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

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

有人能帮我吗?

Jason

3个回答

3

我不知道这是否是问题(您甚至没有说代码如何运行与您期望的运行方式相比),但您的代码中有一些错误:

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

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


我还是缺少什么...你能看到我的最新帖子吗? - Jason
我认为我在建议如何使用$entry()时犯了一个错误。请查看更新后的答案。 - Thomas Broyer
它试图调用方法,但抛出了ClassCastException。alert()显示我正在传递所需的double,但找出要转换的类是不容易的。 - Jason

0

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

$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(thing)是否正确分配了这样的对象,您确定吗?
最后,请再看一眼这个:

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


0

我还是缺少一些东西,而且我已经盯着给定的代码看了相当长的时间。

这是当前版本的代码:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接