1

I would like to implement system of callbacks which looks like this (pseudo code):

final Listener listener = ListenerCtrl.addListener(new Listener() {
  void onNotify(String response){
    ListenerCtrl.unsetListener(listener);
} }

This code mean that after received message, i want to unscribe from future notifications. I found very attractive have this action inside of callback.

Here is my actual implementation:

final WebServiceMsgListener wml = new WebServiceMsgListener()
{
  public void onMsgNotify(JSONObject response, int ecode)
  {
    Log.v(TAG, "getSetStateProgressBar MSG_MGT_STATICINFO: onMsgNotify ecode" +
       ecode);
    authDelegate.unsetMsgListener(wml);
  }
};

authDelegate.addMsgListener(NAOMsg.MSG_MGT_STATICINFO, wml);

Unfortunately, my current implementation show me eclipse error:"The local variable wml may not have been initialized"

Question: how I can get round this, to finally unscribe inside of callback and dont have this error ?

4

1 回答 1

2

将您的代码更改为:

authDelegate.unsetMsgListener(this);

this指当前对象(onMsgNotify()在执行此语句时正在执行该对象)。

注意:虽然该变量wml可用于新对象,但在创建对象时尚未初始化,因此出现错误。它在对象完全创建后立即初始化。

于 2012-06-07T16:51:54.460 回答