设置自定义事件侦听器

时间:2014-03-26 15:32:54

标签: android event-handling

我正在尝试设置自定义事件侦听器,但该标志未被触发。这是相关的代码:

public interface onPlayerSetUpEventListener {
    public void onPlayerSetUp();
}

private onPlayerSetUpEventListener mOnPlayerSetUpEventListener;

public void setOnPlayerSetUpEventListener(onPlayerSetUpEventListener eventListener) {
    mOnPlayerSetUpEventListener = eventListener;
}

public void playerSetUp() {
    if(mOnPlayerSetUpEventListener != null){
        mOnPlayerSetUpEventListener.onPlayerSetUp();
    }
} 

/* The listener is set here */

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    initializeHotels();
    FIRST_TURN = true;
    super.onCreate(savedInstanceState);
    clearOldBoard();
    setContentView(R.layout.activity_game_board);
    setUpBoardGUI();
    setUpPlayers();

    setOnPlayerSetUpEventListener(new onPlayerSetUpEventListener() {
        @Override
        public void onPlayerSetUp(){
            prepForFirstTurn();
        }
    });

/* playerSetUp() should trigger the event */

public void setUpPlayers(){
    <!-- Here we set up all the players and prep them for the start of game -->
    playerSetUp();
}

我对这里的事件处理非常陌生,所以额外的一双眼睛可以帮助我识别发生的事情以及为什么事件没有被执行会很棒!

是不是因为它试图处理onCreate()内的事件?

先谢谢你的帮助!

JRad The Bad

1 个答案:

答案 0 :(得分:1)

在下一行设置侦听器之前,您正在使用setUpPlayers();