Android回调错误

Android Callback Error

我想在ShipInfoManager中做一个回调函数来通知MainActivity做一些动作。

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

有什么方法可以在加载数据后触发回调吗? 或者,有什么方法可以像 Android 的 API 的 LocatioMangerrequestLocationUpdates 那样进行回调,在 data/variables 更改时提供回调?

感谢您的任何回复!

MyCallback 接口:

interface MyCallback {
    void callbackCall();
}

ShipInfoManager class:

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();

    }
}

您的回调似乎没有任何侦听器,您也没有对此进行检查。

您应该将 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() {

    }
}

我像这样更改了我的 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");
        }
    };




}