[英]Calling Java Method from 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);
}
誰能幫我嗎?
賈森
我不知道這是不是問題(你甚至沒有說明代碼的行為與預期的行為有關)但你的代碼中有一些錯誤:
$entry
包裝一個函數,所以你必須調用它返回的函數,而不是(無用地)在調用它之后包裝函數的結果! 即$entry(function(lat,lng) { foo.@bar.Baz::quux(DD)(a, b); }
而不是$entry(foo.@bar.Baz::quux(DD)(a, b))
你在map
上添加了addListener
,但從未定義過該變量。
我能看到的一個錯誤就是你應該這樣做:
$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
我仍然缺少一些東西,而且我已經盯着給定的代碼了很長一段時間。
這是代碼的當前版本:
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。 對不起,如果我錯過了一些顯而易見的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.