首先,是的,我已经搜索过并找到了这个答案:
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);
}
任何人都可以帮助我吗?
杰森
答案 0 :(得分:3)
我不知道这是不是问题(你甚至不知道代码的行为与你预期的行为方式有关)但你的代码中有一些错误:
$entry
包装一个函数,所以你必须调用它返回的函数,而不是(无用地)在调用它之后包装函数的结果!
即$entry(function(lat,lng) { foo.@bar.Baz::quux(DD)(a, b); }
而不是$entry(foo.@bar.Baz::quux(DD)(a, b))
addListener
上的map
,但该变量从未定义过。
答案 1 :(得分: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。对不起,如果我错过了一些显而易见的事情。
答案 2 :(得分: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(事情,你确定在$ wnd上分配了这样的对象吗?
最后,再看一遍:
https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI