尝试调用DatePickerFragment

时间:2018-11-04 22:44:36

标签: android android-studio android-fragments

我正在尝试在Android Studio上显示DatePicker。我有以下代码:

public class PeopleFragment extends AppFragment implements DatePickerDialog.OnDateSetListener {

    //...

    @OnClick(R.id.people_data)
    public void onDateClick() {

        DialogFragment dialogFragment = new DatePickerFragment() {
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                final Calendar c = Calendar.getInstance();
                int day = c.get(Calendar.DAY_OF_MONTH);
                int month = c.get(Calendar.MONTH);
                int year = c.get(Calendar.YEAR);

                return new DatePickerDialog(getActivity(), PeopleFragment.this, year, month, day);
            }
        };

        dialogFragment.show(getActivity().getFragmentManager(), "DatesPicker");  //error when this line is called

}

当我单击按钮以显示日期选择器时,出现错误

java.lang.IllegalStateException: Fragment null must be a public static class to be  properly recreated from instance state.

2 个答案:

答案 0 :(得分:0)

该错误表明DatePickerFragment不是public static类。您的代码大致编译为以下内容。请注意,我留下了所有不相关的代码:

public class PeopleFragment {

    private class InnerDatePickerFragment extends DatePickerFragment {

        private PeopleFragment parent;

        InnerDatePickerFragment(PeopleFragment parent) {
            this.parent = parent;
        }
    }

    public void onDateClick() {
        DialogFragment dialogFragment = new InnerDatePickerFragment(this);
    }

}

所以您的代码有2个问题:

  • 该片段是私有的。在您的情况下,片段类实际上仅在方法内部声明,但是在我的代码示例中使用private足够接近。这也可能是为什么它对Fragment名称说null的原因,因为Fragment类实际上是一个匿名内部类。
  • 该片段引用了外部PeopleFragment

只能使用public static类的限制是Android的限制。由于活动和片段的生命周期,Android必须能够完全重新创建Activites和Fragments实例。在您的情况下,它将无法执行此操作。它无法创建私有/匿名Fragment,并且无法设置隐式parent字段。

您该如何解决?很简单,将DatePickerFragment类移出PeolpeFragment类,或使其成为public static内部类。例如

public class PeopleFragment {

    public static class InnerDatePickerFragment extends DatePickerFragment {
        // ... your code goes here ...
    }

    public void onDateClick() {
        DialogFragment dialogFragment = new InnerDatePickerFragment();

        // ... your code goes here ...
    }
}

答案 1 :(得分:-1)

  

在XML文件中尝试一下:

<EditText
   android:id="@+id/Birthday"
   custom:font="@string/font_avenir_book"
   android:clickable="true"
   android:editable="false"
   android:hint="@string/birthday"/>
  

现在在Java文件中:

Calendar myCalendar = Calendar.getInstance();

EditText edittext= (EditText) findViewById(R.id.Birthday);
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
        myCalendar.set(Calendar.YEAR, year);
        myCalendar.set(Calendar.MONTH, monthOfYear);
        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateLabel();
    }

};

edittext.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        new DatePickerDialog(classname.this, date, myCalendar
                .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                myCalendar.get(Calendar.DAY_OF_MONTH)).show();
    }
});
  

现在在上述活动中添加方法。

private void updateLabel() {
    String myFormat = "MM/dd/yy"; //In which you need put here
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

    edittext.setText(sdf.format(myCalendar.getTime()));
}
  

在EditText的XML文件中添加android:focusable =“ false”以允许单次触摸。