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

前端 未结 9 538
名媛妹妹
名媛妹妹 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.

提交回复
热议问题