繁体   English   中英

在Android中创建自定义提醒对话框时出错

[英]Error while creating custom Alert Dialog in android

Android应用程序中创建自定义Alert Dialog时,出现以下错误。

错误

requestFeature() must be called before adding content

以下是我的自定义Alert Dialog创建代码。

AlertDialog alertDialog=new AlertDialog.Builder(home.this).create();
alertDialog.setTitle("Title here..");
alertDialog.setContentView(R.layout.custom_alertdialog);
alertDialog.show(); 

自定义对话框的代码段如下:

使用新的Dialog而不是DialogBu​​ilder

Dialog d = new Dialog(MainActivity.this);
        d.setContentView(R.layout.dialog);
        d.setTitle("This is custom dialog box");
        d.show();   

从第一行删除.create()。

以此为指导:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    YourActivity.this);

alertDialogBuilder.setTitle("Your title here...");

alertDialogBuilder
        .setMessage("Your message here...")
        .setCancelable(false)
        .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // Your works
                }
            })
        .setNegativeButton("No",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                dialog.cancel();
            }
        });

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.setContentView(R.layout.custom_alertdialog);
alertDialog.show();

尝试这个 :

AlertDialog.Builder alertDialogBuilder=new AlertDialog.Builder(home.this);
alertDialogBuilder.setTitle("Title here..");
alertDialogBuilder.setContentView(R.layout.custom_alertdialog);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show(); 

暂无
暂无

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

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