5

GWT プロジェクトでいくつかの関数フックを提供しようとしています。

private TextBox hello = new TextBox();
private void helloMethod(String from) { hello.setText(from); }
private native void publish() /*-{
 $wnd.setText = $entry(this.@com.example.my.Class::helloMethod(Ljava/lang/String;));
}-*/;

publish()で呼び出されonModuleLoad()ます。しかし、これは機能せず、開発コンソールで理由に関するフィードバックを提供しません。私も試しました:

private native void publish() /*-{
 $wnd.setText = function(from) {
  alert(from);
  this.@com.example.my.Class::helloMethod(Ljava/lang/String;)(from);
 }
}-*/;

これはjava.lang.ClassCastExceptionFireBug コンソールでa を投げますが、問題alertなく発火します。提案?

4

2 に答える 2

7

helloMethodはインスタンス メソッドであるため、呼び出されthisたときに参照を設定する必要があります。最初の例では、呼び出し時にこれを行いません。2 番目の例ではこれを実行しようとしていますが、JavaScript で簡単に作成できる小さな間違いがあります。this

$wnd.setText = function(from) {
  this.@com.example.my.Class::helloMethod(Ljava/lang/String;)(from);
};

関数自体を指します。これを回避するには、次のようにする必要があります。

var that = this;
$wnd.setText = function(from) {
  that.@com.example.my.Class::helloMethod(Ljava/lang/String;)(from);
};

またはより良い:

var that = this;
$wnd.setText = $entry(function(from) {
  that.@com.example.my.Class::helloMethod(Ljava/lang/String;)(from)
});
于 2011-03-08T16:50:32.737 に答える
7
private native void publish(EntryPoint p) /*-{
 $wnd.setText = function(from) {
  alert(from);
  p.@com.example.my.Class::helloMethod(Ljava/lang/String;)(from);
 }
}-*/;

このコードを試していただけますか?

于 2011-03-08T17:00:12.303 に答える