AlertDialog将不会显示在onCreate方法中

坏人杰拉德

因此,在继续构建活动之前,我想问一个用户他们的名字。大多数活动都是动态填充的,因此看起来应该很容易。由于某种原因,对话框永远不会出现。我已经尝试了所有方法,唯一想到的是:也许不喜欢使用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);

}
Rod_Algonquin

create方法返回一个实例AlertDialog

调用AlertDialog的create方法时,无需将其初始化为Dialog

Dialog dialog = startGameDialog.create();
dialog.show();

将其传递给AlertDialog

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

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

Context context = Your_activity.this;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章