简体   繁体   中英

Error while creating custom Alert Dialog in android

I am getting the following error while creating a custom Alert Dialog in Android application.

Error

requestFeature() must be called before adding content

Following is my code for the custom Alert Dialog creation.

Code

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

Code snippet here for custom Dialog:

use new Dialog instead of DialogBuilder

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

从第一行删除.create()。

Use this as guide:

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

Try this :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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