Android回调错误

时间:2015-01-23 16:26:08

标签: java android interface callback

我想在ShipInfoManager中设置回调函数,以通知MainActivity执行某些操作。

如果我将onEvent()放入Runnable,它就会运行。 但是,如果我这样说,它会显示错误。

加载数据后是否有办法触发回调? 或者,有没有办法像Android的LocatioManger requestLocationUpdates一样进行回调,在数据/变量发生变化时进行回调?

感谢您的回复!

MyCallback界面

interface MyCallback {
    void callbackCall();
}

ShipInfoManager类

public class ShipInfoManager {

    Context mContext;

    public ShipInfoManager(Context _mContext) {
        this.mContext = _mContext;

        reloadData();
        startTimer();
    }

    MyCallback callback;

    void onEvent() {
        callback.callbackCall();
    }

    private void startTimer() {
             /* RUN EVERY MIN */
        final Handler handler = new Handler();
        Timer timer = new Timer();
        TimerTask doAsynchronousTask = new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @SuppressWarnings("unchecked")
                    public void run() {
                        try {
                            reloadData();

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        };
        // TEMP SUSPEND FROM PREVENTING RELOAD //
        timer.schedule(doAsynchronousTask, 0, 5000);

    }

    /* JSON handling and extraction */
    private void reloadData() {
        //Do sth to reload the data

        //After reload, I want to fire the callback
        onEvent();

    }
}

2 个答案:

答案 0 :(得分:0)

看起来你的回调没有听众,你也没有检查过。

您应该将ShipInfoManager替换为:

public class ShipInfoManager {
    public interface MyCallback {
        void callbackCall();
    }

    public void setCustomEventListener(MyCallback eventListener) {
       callback = eventListener;
    }

    Context mContext;

    public ShipInfoManager(Context _mContext)  {
        this.mContext = _mContext;

        reloadData();
        startTimer();
    }

    MyCallback callback;

    void onEvent() {
        // Check if we have listeners 
        if (callback != null)
            callback.callbackCall();
    }

    private void startTimer() {
         /* RUN EVERY MIN */
        final Handler handler = new Handler();
        Timer timer = new Timer();
        TimerTask doAsynchronousTask = new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @SuppressWarnings("unchecked")
                    public void run() {
                        try {
                            reloadData();

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        };
        // TEMP SUSPEND FROM PREVENTING RELOAD //
        timer.schedule(doAsynchronousTask, 0, 5000);

    }

    /* JSON handling and extraction */
    private void reloadData() {
        //Do sth to reload the data

        //After reload, I want to fire the callback
        onEvent();

    }
}

ActivityFragment内,你应该有类似的内容:

public class MainActivity extends ActionBarActivity implements ShipInfoManager.MyCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ShipInfoManager s = new ShipInfoManager(this);
        s.setCustomEventListener(this);
    }

    @Override
    public void callbackCall() {

    }
}

答案 1 :(得分:0)

我改变了我的MainActivity。它现在有效。 谢谢你的建议和回复!!!!!

public class MainActivity extends ActionBarActivity   {


ShipInfoManager mShipInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mShipInfo = new ShipInfoManager(this);


    Log.i("Show Ship List Size", String.valueOf(mShipInfo.get_shipsList().size()));
    Log.i("Show Ship - 6", String.valueOf(mShipInfo.getShip(6).getShip_name()));



    mShipInfo.callback = new ShipInfoManager.MyCallback() {
        @Override
        public void callbackCall() {
            Log.i("Call Back", "it is called");
        }
    };




}