AlertDialog不会在onCreate方法中显示

时间:2014-06-12 01:03:36

标签: android dialog oncreate

所以我想在继续构建活动之前询问用户他们的名字。大多数活动是动态填充的,因此看起来应该很容易。出于某种原因,Dialog从未出现过。我已经尝试了所有的东西,我唯一能想到的是:也许它不喜欢加入onCreate方法?看起来这应该是一个问题,因为它确实是onCreate中调用的最后一个方法。看看它,让我知道你看到了什么:

onCreate方法:

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

    initializeHotels();
    FIRST_TURN = true;

    clearOldBoard();
    setContentView(R.layout.activity_game_board);
    setUpBoardGUI();

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

    startGameDialog();
}

和startGameDialog方法:

public void startGameDialog(){
    Context context = getApplicationContext();

    ContextThemeWrapper ctw = new ContextThemeWrapper(context, R.style.AppBaseTheme);

    AlertDialog.Builder startGameDialog = new AlertDialog.Builder(ctw);
    startGameDialog.setTitle(getResources().getString(R.string.whats_your_name));

    LinearLayout dialogLayout = new LinearLayout(context);

        final EditText newName = new EditText(context);
        newName.setText("");

        Button submit = new Button(context);
        OnClickListener onClick = new OnClickListener() {

            @Override
            public void onClick(View v) {
                GameBoardActivity.NAME = newName.toString();
                setUpPlayers();

            }
        };
        submit.setOnClickListener(onClick);

    dialogLayout.addView(newName);
    dialogLayout.addView(submit);

    startGameDialog.setView(dialogLayout);
    Dialog dialog = startGameDialog.create();
    dialog.show();
    dialog.setCancelable(false);

}

1 个答案:

答案 0 :(得分:2)

create方法返回AlertDialog

的实例

当您调用AlertDialog的create方法

时,不要将其初始化为Dialog
Dialog dialog = startGameDialog.create();
dialog.show();

将其传递给AlertDialog

AlertDialog alertDialog = startGameDialog.create();
alertDialog.show();

使用current活动上下文而不是使用整个应用程序contex.t

Context context = Your_activity.this;