【问题标题】:Cannot cast from DialogFragment to View - any workarounds?无法从 DialogFragment 投射到 View - 任何解决方法?
【发布时间】:2012-08-12 15:49:31
【问题描述】:
我在这些代码行中,eclipse 在第三行给了我一个错误,如下所示:“无法将 DialogFragment 转换为 View”,有什么解决方法吗?或其他选项/选项?
DialogFragment dateClock = new DatePickerFrag();
View homeSection = findViewById(R.layout.introducere_date);
((LinearLayout) homeSection).addView(((View) dateClock));
【问题讨论】:
标签:
android
date
casting
dialog
fragment
【解决方案1】:
这将是显示 TimePickerDialog 的一种方法 -
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}
然后
public void showTimePickerDialog() {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}