在 DialogFragment 中显示 DatePickerDialog

Show DatePickerDialog in DialogFragment

是否可以在 DialogFragment 之上添加 DatePickerDialog?

我有一个带有用于添加日期的 EditText 的 DialogFragment。当 onFocus 时,我希望它显示一个 DatePickerDialog。对于我当前的实现,它在应用程序尝试显示 DatePickerDialog 时崩溃。

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

public class AddProjectDialogFragment extends DialogFragment {
    private Context mContext;

    public AddProjectDialogFragment() {
        // Required empty public constructor
    }

    public static AddProjectDialogFragment newInstance() {
        return new AddProjectDialogFragment();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_add_project_dialog, container, false);
        mContext = getActivity().getApplicationContext();
        configureView(view);

        return view;
    }

    private void configureView(View view) {
        TextInputEditText projectNameET = (TextInputEditText) view.findViewById(R.id.project_name_et);
        final EditText dateET = (EditText) view.findViewById(R.id.date_picker_et);
        AppCompatButton addProjectBtn = (AppCompatButton) view.findViewById(R.id.add_project_btn);

        final Calendar cal = Calendar.getInstance();
        final DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                String dateFormat = "dd/MM/yyyy";
                SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
                dateET.setText(sdf.format(cal.getTime()));
            }
        };

        dateET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                DatePickerDialog dpd = new DatePickerDialog(mContext, dateSetListener,
                        cal.get(Calendar.YEAR),
                        cal.get(Calendar.MONTH),
                        cal.get(Calendar.DAY_OF_MONTH));

                if (b) {
                    dpd.show();
                } else {
                    dpd.dismiss();
                }
            }
        });
    }
}

你得到 WindowManager$BadTokenException,所以你必须使用

 mContext = getActivity();

从您的 mContext

中删除 getApplicationContext()

你也可以这样用,我这边很管用

 dateET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            DatePickerDialog dpd = new DatePickerDialog(getActivity(), dateSetListener,
                    cal.get(Calendar.YEAR),
                    cal.get(Calendar.MONTH),
                    cal.get(Calendar.DAY_OF_MONTH));

            if (b) {
                dpd.show();
            } else {
                dpd.dismiss();
            }
        }
    });