Android AlertDialog没有显示

时间:2018-04-19 00:16:26

标签: android alertdialog

我无法显示警告对话框。我把它放到了调试模式,它完成了所有事情,然后即使调试器处理该行,也不会显示菜单,然后转到下一段代码。

if(game.checkForPromotion(startRow, startCol)){
    AlertDialog.Builder builder = new AlertDialog.Builder(GameActivity.this);
    builder.setTitle("Pick a piece")
    .setItems(R.array.pieces_array, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // The 'which' argument contains the index position
            // of the selected item
        }
    });
    builder.create().show();
    Log.d("GameActivity: ", "Is it crashing before this?");
}

Log.d("GameActivity: ", "Totally done with alert");

// More Code

2 个答案:

答案 0 :(得分:0)

我使用您的源代码运行,我将 [R.array.pieces_array] 的值更改为我的变量 [final CharSequence [] items = {“这是setItems的内容”} ] 即可。 我运行程序和警告对话框显示确定。 源代码显示对话框确定:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final CharSequence[] items = {"This is content of setItems"};
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Pick a piece")
            .setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // The 'which' argument contains the index position
                    // of the selected item
                }
            });
    builder.create().show();
    Log.d("GameActivity: ", "Is it crashing before this?");
}

我认为您需要检查文件中的其他源代码。

答案 1 :(得分:0)

试试这个,它对我有用

new AlertDialog.Builder(getContext(), android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar_MinWidth)
                    .setTitle("Title")
                    .setMessage("My message")
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            // get ok button click

                        }
                    })
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            // get cancel button click
                            dialog.dismiss();
                        }
                    })
                    .show();