无法从DialogFragment转换为View - 任何变通办法?

时间:2012-08-12 15:49:31

标签: android date casting dialog fragment

在这些代码行中,eclipse在第三行中给出了一个错误,如下所示:“无法将DialogFragment强制转换为View”,这有什么解决方法吗?或其他选项/选项?

DialogFragment dateClock = new DatePickerFrag();
View homeSection = findViewById(R.layout.introducere_date);
((LinearLayout) homeSection).addView(((View) dateClock));

1 个答案:

答案 0 :(得分: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");
}