【问题标题】: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");
    }
    

    【讨论】:

    • 非常感谢您的直截了当的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多