我正在尝试在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.
答案 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”以允许单次触摸。