[英]Calling Java Method from GWT JSNI
First, yes I have searched already and found this answer already: 首先,是的,我已经搜索过,已经找到了这个答案:
GWT JSNI - problem passing Strings GWT JSNI - 传递字符串的问题
I'm trying to call a java method from a JSNI method, but not getting anywhere. 我试图从JSNI方法调用java方法,但没有到达任何地方。 I've tried the advice given above, but it still won't work. 我已经尝试了上面给出的建议,但它仍然无效。
Here's the code: 这是代码:
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);
}
Can anyone help me out? 谁能帮我吗?
Jason 贾森
I don't know if that's the issue (you don't even say how that code behaves vs. how you expect it to behave) but there are a few bugs in your code: 我不知道这是不是问题(你甚至没有说明代码的行为与预期的行为有关)但你的代码中有一些错误:
$entry
wraps a function, so you have to call the function it returns, rather than (uselessly) wrap the result of the function after calling it! $entry
包装一个函数,所以你必须调用它返回的函数,而不是(无用地)在调用它之后包装函数的结果! Ie $entry(function(lat,lng) { foo.@bar.Baz::quux(DD)(a, b); }
rather than $entry(foo.@bar.Baz::quux(DD)(a, b))
即$entry(function(lat,lng) { foo.@bar.Baz::quux(DD)(a, b); }
而不是$entry(foo.@bar.Baz::quux(DD)(a, b))
you addListener
on a map
but that variable is never defined. 你在map
上添加了addListener
,但从未定义过该变量。
One mistake I can see is that you should do: 我能看到的一个错误就是你应该这样做:
$wnd.getLocationDescriptions = $entry(@org.jason.mapmaker.client.view.MapmakerMapViewImpl::getLocationDescriptions(DD)(lng, lat));
(not with the function 'wrapper' you use), and then call the function from javascript via: (不使用你使用的函数'wrapper'),然后通过javascript调用函数:
$wnd.getLocationDescriptions(lng, lat);
Also, the 'that' variable is not necessary (or 'this'), before the @. 此外,在@之前,'that'变量不是必需的(或'this')。 I'm also not sure about the $wnd.google.maps.event.addListener( thing, are you sure there's such an object assigned on $wnd? 我也不确定$ wnd.google.maps.event.addListener(事情,你确定在$ wnd上分配了这样的对象吗?
Last, take one more look at this: 最后,再看一遍:
https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI
I'm still missing something, and I've stared at the given code for quite awhile. 我仍然缺少一些东西,而且我已经盯着给定的代码了很长一段时间。
Here's the current version of the code: 这是代码的当前版本:
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()");
});
}-*/;
The call is resulting in a ClassCastException. 该调用导致ClassCastException。 Sorry if I've missed something that should be obvious. 对不起,如果我错过了一些显而易见的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.