0

我的 Android 应用程序显示一个 AlertDialog,第一次一切正常。但是,在显示然后取消对话框后,它第二次在 show() 上崩溃。我尝试了类似帖子中提出的建议,但没有任何帮助。这是我的代码:

    addButton.setOnClickListener(this);
}

@Override
public void onClick (View view) {
    switch (view.getId()) {
    case R.id.config_add: 
        builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.create_content_server_config_title);
        builder.setView(config_view);
        builder.setPositiveButton(R.string.dialog_confirm_create_config_button, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // User clicked Create button
                Toast.makeText(ManageContentServers.this, config_name.getText().toString(), Toast.LENGTH_LONG).show();
            }
        });
        builder.setNegativeButton(R.string.dialog_config_cancel_button, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // User cancelled the dialog
                dialog.dismiss();
            }
        });
        dialog = builder.create();
        dialog.show();
        break;
    }
}
4

2 回答 2

0

Its because you're using "setView". You're need to recreate the dialog everytime!

 private void showDialog() { 
    builder = new AlertDialog.Builder(this); builder.setTitle(R.string.create_content_server_config_title); builder.setView(config_view);  builder.setPositiveButton(R.string.dialog_confirm_create_config_button, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User clicked Create button
                        Toast.makeText(ManageContentServers.this, config_name.getText().toString(), Toast.LENGTH_LONG).show();
                    }
                });
                builder.setNegativeButton(R.string.dialog_config_cancel_button, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                        dialog.dismiss();
                    }
                    });
                    dialog = builder.create();
                    dialog.show(); 
        }


    case R.id.config_add:
    showDialog();
    break;
于 2013-04-30T19:46:03.737 回答
0

我不确定你是不是在说同样的话,但我通过移动更轻松地解决了问题

config_view = inflater.inflate(R.layout.ecs_config_fields, null);

从 onCreate() 方法到 onClick(),就在 builder.setView(config_view) 之前;

于 2013-04-30T19:59:33.617 回答