Dialog problem: requestFeature() must be called before adding content

Also make sure you are not returning an already-shown dialog. For example, ProgressDialog has a handy static method show() which takes some parameters and returns a ProgressDialog. Turns out you can't use that method and return the resulting ProgressDialog, because then when the OS tries to show it (and it's already shown), it will throw this same exception.

Beware, too: the above behavior is experienced on the Android emulator, but it actually did not throw an exception and worked just fine on my Droid Incredible running 2.2.


You need to set the custom view before creating the dialog. Also you need to use setView(View) instead of setContentView() if you are using the default positive and negative buttons provided for you by the AlertDialog.

final EditText newKey = (EditText) findViewById(R.id.dialog_result);
AlertDialog.Builder keyBuilder = new AlertDialog.Builder(StegDroid.this);
keyBuilder
.setCancelable(false)
.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        Log.v("Dialog","New Key: "+newKey.getText().toString());
    }
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });
keyBuilder.setTitle("Decryption Failed");
keyBuilder.setView(getLayoutInflater().inflate(R.layout.decrypt_failed_dialog, null));
AlertDialog dialog = keyBuilder.create();
dialog.show();

Tags:

Android

Dialog