error: requestFeature() must be called before adding content - Still won't work

后端 未结 3 640
抹茶落季
抹茶落季 2021-01-20 21:06

I know that similar questions have been asked in the past but I can\'t seem to get this working at all even with the suggestions.

I get the above abend on the \"show

3条回答
  •  执笔经年
    2021-01-20 21:17

    I experienced the same problem. I found that the problem only occurs if I do both of the following things:

    • I don't use activity managed dialogs (activity.showDialog() -> activity.onCreateDialog()/onPrepareDialog())

    • I do dialog.findViewById() (and this is indeed the line difference between success or the requestFeature exception!).

    final Builder dialogBuilder = new AlertDialog.Builder(activity);
    b.setView(rootView);
    b.setIcon(android.R.drawable.ic_dialog_info);
    b.setTitle(R.string.message_of_the_day_title);
    b.setCancelable(false);
    dialog = b.createDialog();
    dialog.findViewById(R.id.myid); // this is the problem
    

    The dialog.findViewById() causes the problem because it calls

    dialog.getWindow().getDecorView()
    

    and the method javadoc of getDecorView() says:

    Note that calling this function for the first time "locks in" various window characteristics as described in {@link #setContentView(View, android.view.ViewGroup.LayoutParams)}.

    Isn't that nice, findViewById() has a side effect which causes seemingly correct applications to crash. Why there's a difference between Activity managed dialogs and normal dialogs I do not know, but I guess getDecorView() does some magic for Activity managed dialogs.

    I did the above because I moved from using Activity managed dialogs to handling dialogs myself.

    The solution for me is to manipulate the rootView, using rootView.findViewById(), instead of manipulating the dialog.

提交回复
热议问题