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

前端 未结 9 506
名媛妹妹
名媛妹妹 2020-12-31 02:39

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_r         


        
相关标签:
9条回答
  • 2020-12-31 03:17

    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:

    public class MyDialogFragment extends DialogFragment {
        private View mLayout;
        private ViewGroup mContainer;
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            mContainer = container;
            if (getShowsDialog()) {
                // one could return null here, or be nice and call super()
                return super.onCreateView(inflater, container, savedInstanceState);
            }
            return getLayout(inflater, container);
        }
    
        private View getLayout(LayoutInflater inflater, ViewGroup container) {
            mLayout = inflater.inflate(R.layout.my_layout, container, false);
            return mLayout;
        }
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return new AlertDialog.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.

    0 讨论(0)
  • 2020-12-31 03:19

    This also happens if you try to access any view item with findViewById() before the onCreate() That is in my case i did:

     private class ShareViaDialog extends Dialog  {
     private ImageView mainSmallIcon = findViewById(R.id.mainSmallIcon);    //this is wrong
      @Override
            protected void onCreate(Bundle savedInstanceState) {
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                super.onCreate(savedInstanceState);
    }}
    

    so the correct way to avoid this error is:

     private class ShareViaDialog extends Dialog  {
         private ImageView mainSmallIcon;
          @Override
                protected void onCreate(Bundle savedInstanceState) {
                    requestWindowFeature(Window.FEATURE_NO_TITLE);
                    super.onCreate(savedInstanceState);
    mainSmallIcon = findViewById(R.id.mainSmallIcon); //should be here
        }}
    
    0 讨论(0)
  • 2020-12-31 03:27

    I also had this error message when using a DialogFragment.

    My problem was solved when in the onCreateDialog(Bundle savedInstanceState) I changed the dialog that I returned

    from

    android.appAlertDialog

    to

    androidx.appcompat.app.AlertDialog

    (or if you haven't migrate to AndroidX, then android.support.v7.app.AlertDialog)

    0 讨论(0)
  • 2020-12-31 03:33

    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).

    0 讨论(0)
  • 2020-12-31 03:35

    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();
    
    0 讨论(0)
  • 2020-12-31 03:35

    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.

    0 讨论(0)
提交回复
热议问题