这是我的片段代码,我试图使用DialogFragment调用datepicker
public class ExpensesDaily extends Fragment{
public ExpensesDaily(){}
Button button;
Calendar now;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.expenses_daily, container, false);
Button button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment picker = new DatePickerFragment();
picker.show(getFragmentManager(), "datePicker");
}
});
return rootView;
}
}
这是我的DatePickerFragment代码
public class DatePickerFragment extends DialogFragment {
DatePickerDialog.OnDateSetListener onDateSet;
TextView tvDate;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), onDateSet, year, month, day);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.expenses_daily, container, false);
tvDate = (TextView) rootView.findViewById(R.id.textViewDate);
return rootView;
}
public void setCallBack(DatePickerDialog.OnDateSetListener onDate) {
onDateSet = onDate;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
tvDate.setText("date: " + formattedDate);
}
}
但每次我点击按钮显示日期选择器时,应用程序都会崩溃。指向仍然不完整的指针?
更新:我正在尝试按照这篇文章http://www.vogella.com/tutorials/AndroidFragments/article.html来实现,但找不到适合做datepicker的方法
感谢
答案 0 :(得分:2)
好的,这就是我的表现。我在这里粘贴我的整个代码,以帮助任何人与我有同样的问题。我在这里引用这篇文章http://www.solardriftsolutions.co.uk/android-fragment-and-dialogfragment/来修复requestFeature()错误和文章http://androidtrainningcenter.blogspot.com/2012/10/creating-datepicker-using.html以获取日期选择器设置的值
DatePickerFragment.java的代码,它扩展了DialogFragment
public class DatePickerFragment extends DialogFragment {
DatePickerDialog.OnDateSetListener onDateSet;
private boolean isModal = false;
public static DatePickerFragment newInstance()
{
DatePickerFragment frag = new DatePickerFragment();
frag.isModal = true; // WHEN FRAGMENT IS CALLED AS A DIALOG SET FLAG
return frag;
}
public DatePickerFragment(){}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), onDateSet, year, month, day);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(isModal) // AVOID REQUEST FEATURE CRASH
{
return super.onCreateView(inflater, container, savedInstanceState);
}
else {
View rootView = inflater.inflate(R.layout.expenses_daily, container, false);
return rootView;
}
}
public void setCallBack(DatePickerDialog.OnDateSetListener onDate) {
onDateSet = onDate;
}
}
这里是ExpensesDaily.java的代码,它是一个调用datepickerfragment的片段
public class ExpensesDaily extends Fragment{
public ExpensesDaily(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.expenses_daily, container, false);
Button button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerFragment dpf = new DatePickerFragment().newInstance();
dpf.setCallBack(onDate);
dpf.show(getFragmentManager().beginTransaction(), "DatePickerFragment");
//DialogFragment picker = new DatePickerFragment();
//picker.show(getFragmentManager(), "datePicker");
}
});
return rootView;
}
DatePickerDialog.OnDateSetListener onDate = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
TextView tvDate = (TextView) getActivity().findViewById(R.id.tvDate);
tvDate.setText(String.valueOf(year) + "-" + String.valueOf(monthOfYear)
+ "-" + String.valueOf(dayOfMonth));
}
};
}