从GWT JSNI调用Java方法
首先,是的,我已经搜索并找到了这个答案: 我试图从JSNI方法调用java方法,但没有成功。我试过上面的建议,但还是不管用 代码如下:从GWT JSNI调用Java方法,java,gwt,Java,Gwt,首先,是的,我已经搜索并找到了这个答案: 我试图从JSNI方法调用java方法,但没有成功。我试过上面的建议,但还是不管用 代码如下: public native void initCustomListeners(MapmakerMapViewPresenter.MyView view) /*-{ //public native void initCustomListeners() /*-{ $wnd.getLocationDescriptions = function(lng, l
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);
}
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()");
});
}-*/;
有人能帮我吗
Jason我不知道这是否是问题所在(您甚至没有说明代码的行为与预期的行为),但您的代码中存在一些错误:
包装函数,因此您必须调用它返回的函数,而不是(无用地)在调用函数后包装函数的结果! 例如,$entry
而不是$entry(函数(lat,lng){foo@bar.Baz::qux(DD)(a,b);}
$entry(foo@bar.Baz::qux(DD)(a,b))
- 您可以在
上添加侦听器,但该变量从未定义map
- 我仍然缺少一些东西,我已经盯着给定的代码看了很久了
以下是代码的当前版本:
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);
}
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。如果我错过了一些显而易见的内容,请道歉。我看到的一个错误是您应该:
$wnd.getLocationDescriptions = $entry(@org.jason.mapmaker.client.view.MapmakerMapViewImpl::getLocationDescriptions(DD)(lng, lat));
(不使用您使用的函数“wrapper”),然后通过以下方式从javascript调用该函数:
$wnd.getLocationDescriptions(lng, lat);
另外,@.I也不确定$wnd.google.maps.event.addListener之前的'that'变量是不必要的(或'this')(事情,你确定$wnd上分配了这样的对象吗
最后,再看一眼:
我仍然缺少一些东西…你能看到我最近的帖子吗?我认为我建议使用
$entry()
的方式有错误。请查看更新的答案。它试图调用该方法,但抛出了ClassCastException。警报()显示我正在按要求传递一个double,但找出试图被强制转换的类并不重要。