在片段中的片段中加载日期选择器

时间:2019-07-01 12:55:20

标签: javascript java android android-studio

我有一个小问题,我正在尝试使用一个片段将日期选择器实现到另一个片段中,但是当我单击按钮以加载日期选择器时,我收到了此错误消息,我在日志中得到了该错误

java.lang.ClassCastException: com.example.buzzamaid.HomeActivity cannot be cast to android.app.DatePickerDialog$OnDateSetListener
        at com.example.buzzamaid.Fragments.DatePickerFragment.onCreateDialog(DatePickerFragment.java:23)

我假设它试图将其传递给HomeActivity而不是我创建的DateTimeFragment。我该如何解决?

我的日期选择器片段


    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        Calendar c = Calendar.getInstance();
        int year =  c.get(Calendar.YEAR);
        int month =  c.get(Calendar.MONTH);
        int day =  c.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getContext(),(DatePickerDialog.OnDateSetListener) getContext(), year, month, day);

    }
}

MyDateTimeFragment



    Unbinder unbinder;

    @BindView(R.id.bt_select_date)
    Button bt_select_date;

    @BindView(R.id.bt_select_time)
    Button bt_select_time;

    @BindView(R.id.tv_selected_date)
    TextView tv_selected_date;

    @BindView(R.id.tv_selected_time)
    TextView tv_selected_time;



    static DateTimeFragement instance;

    public  static DateTimeFragement getInstance() {
        if (instance == null)
            instance = new DateTimeFragement();
        return instance;

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View itemView =  inflater.inflate(R.layout.fragment_date_time,container,false);
        unbinder = ButterKnife.bind(this,itemView);

        return itemView;



    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);




    }


    @OnClick(R.id.bt_select_date)
    public void onClick() {
        DialogFragment datePicker = new DatePickerFragment();
        datePicker.show(getFragmentManager(), "Date Picker");
    }






    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        String currentDateString = DateFormat.getDateInstance(DateFormat.FULL).format(c.getTime());
        tv_selected_date.setText(currentDateString);
    }
}

当我单击片段中的按钮时,如何使其显示压光机而不崩溃?

2 个答案:

答案 0 :(得分:0)

如果您已将OnDateSetListener实施为DatePickerFragment,请将DatePickerDialog实例更改为:

return new DatePickerDialog(getContext(), DatePickerFragment.this , year, month, day);

答案 1 :(得分:0)

您的HomeActivity正在实现DatePickerDialog.OnDateSetListener接口吗?如果不是,请确保您的HomeActivity实现DatePickerDialog.OnDateSetListener。实现此方法将为您提供覆盖方法

@Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

    }

您可以根据需要覆盖此方法。