2011-09-06 69 views
4

首先调用Java方法,是的,我已经搜查,发现了这个答案已经:从GWT JSNI

GWT JSNI - problem passing Strings

我试图调用从JSNI方法的Java方法,但不是任何地方获得。我已经尝试了上面给出的建议,但它仍然行不通。

下面的代码:

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

    $wnd.getLocationDescriptions = function(lng, lat) { 
     $entry([email protected]::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); 
} 

谁能帮助我吗?

杰森

回答

3

我不知道这是个问题(你甚至不说,代码的行为对你如何指望它来表现),但也有代码中的一些错误:

  • $entry包装一个函数,所以你必须调用它返回的函数,而不是(无用地)在调用它后包装函数的结果! 也就是说$entry(function(lat,lng) { [email protected]::quux(DD)(a, b); }而不是$entry([email protected]::quux(DD)(a, b))

  • addListenermap但从来没有定义的变量。

+0

我仍然错过了一些东西......你能看到我最近的帖子吗? – Jason

+0

我认为在我建议使用'$ entry()'的时候出现了一个错误,请看更新后的答案 –

+0

它试图调用这个方法,但抛出一个ClassCastException。alert()表明我按照需要传递了一个double,但是找出正在尝试传输的Class并不重要。 – Jason

0

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

下面的代码的当前版本:

public native void initCustomListeners(JavaScriptObject map) /*-{ 

    var that = this; 

    $wnd.getLocationDescriptions = function(lng, lat) { 
     $entry([email protected]er::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。对不起,如果我错过了应该是显而易见的东西。

0

一个错误,我可以看到的是,你应该做的:

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

(不与功能“包装”您使用),然后通过从JavaScript调用该函数:

$wnd.getLocationDescriptions(lng, lat); 

另外,在@之前,'that'变量不是必需的(或'this')。我也不能确定$ wnd.google.maps.event.addListener(的东西,你肯定有这样的分配上$ WND对象

最后,需要多一个看看这个:

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