Skip to content Skip to sidebar Skip to footer

Dialog Problem: Requestfeature() Must Be Called Before Adding Content

I'm creating a custom dialog containing an EditText so that I can get text data from the user: final EditText newKey = (EditText) findViewById(R.id.dialog_result); AlertDialog.Buil

Solution 1:

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.

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

Solution 2:

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.

Solution 3:

I've ran into this about a year ago and fixed it in a way using weird code. Again, an app I'm working on has a fragment that should display normally on phone, but in a dialogue on tablet. I solved it like this:

publicclassMyDialogFragmentextendsDialogFragment {
    privateView mLayout;
    privateViewGroup mContainer;

    @Nullable@OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContainer = container;
        if (getShowsDialog()) {
            // one could return null here, or be nice and call super()returnsuper.onCreateView(inflater, container, savedInstanceState);
        }
        returngetLayout(inflater, container);
    }

    privateViewgetLayout(LayoutInflater inflater, ViewGroup container) {
        mLayout = inflater.inflate(R.layout.my_layout, container, false);
        return mLayout;
    }

    @OverridepublicDialogonCreateDialog(Bundle savedInstanceState) {
        returnnewAlertDialog.Builder(getContext())
                .setPositiveButton(R.string.ok, null)
                .setNegativeButton(R.string.cancel, null)
                .setNeutralButton(R.string.filter_clear_selection, null)
                .setView(getLayout(LayoutInflater.from(getContext()), mContainer))
                .create()
            ;
    }
}

This allows me to add my fragment as any normal fragment (in a layout), but also display it as a dialog where it runs autonomously.

Solution 4:

For those who are having this problem specifically when using DialogFragment.

In my case, this was happened because I was extending the DialogFragment class incorrectly. I was returning a view on onCreateView() AND ALSO returning a custom dialog on onCreateDialog(). The documentation states:

You might have a UI design in which you want a piece of the UI to appear as a dialog in some situations, but as a full screen or embedded fragment in others (perhaps depending on whether the device is a large screen or small screen). The DialogFragment class offers you this flexibility because it can still behave as an embeddable Fragment.

However, you cannot use AlertDialog.Builder or other Dialog objects to build the dialog in this case. If you want the DialogFragment to be embeddable, you must define the dialog's UI in a layout, then load the layout in the onCreateView() callback.

Solved the problem by not creating anything on onCreateDialog() (only returning the superclass implementation).

Solution 5:

I had an Alert dialog in my app and it used to give me this android runtime exception on android API 23.

Turned out this problem was with the import. I changed android.app.AlertDialog to androidx.appcompat.app.AlertDialog in the imports and the problem was solved.

This is for projects that use AndroidX Artifacts. Consider using the Support version android.support.v7.app.AlertDialog if you are not using AndroidX.

Post a Comment for "Dialog Problem: Requestfeature() Must Be Called Before Adding Content"