简体   繁体   English

Android AlertDialog未显示

[英]Android AlertDialog not showing

I can't get my alert dialog to show. 我无法显示警报对话框。 I've put it into debug mode and it goes through the whole thing, then never displays the menu even though the debugger processes that line, and moves on to the next bit of code. 我将其置于调试模式,它贯穿了整个过程,然后即使调试器处理该行并继续进行下一段代码,也从不显示菜单。

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

I run with your source code, I change value of [R.array.pieces_array] to my variable [final CharSequence[] items = {"This is content of setItems"};] . 我使用您的源代码运行,将[R.array.pieces_array]的值更改为我的变量[final CharSequence [] items = {“这是setItems的内容”};] I run program and alert dialog show OK. 我运行程序,警报对话框显示OK。 Source code show dialog OK: 源代码显示对话框确定:

@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?");
}

I think you need check other source code in file. 我认为您需要检查文件中的其他源代码。

Try this, it works for me 试试这个,对我有用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM