Tab Fragment错误中创建DatePicker和TimePicker

Create a DatePicker and TimePicker in the Tab Fragment error

我正在尝试在 tab 片段中创建 date pickernumber picker,但出现错误。我无法弄清楚错误或者我做错了因为 extends fragment 与 extends AppCompatActivity??

不同

有人可以指出我缺少什么吗?

 public class KeyInWeightF extends Fragment implements View.OnClickListener {

        View contentView;
        EditText btnTime;
        EditText btnDate;
    TimePickerDialog timePickerDialog;
    DatePickerDialog datePickerDialog;

    Calendar calender = Calendar.getInstance();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        contentView=inflater.inflate(R.layout.daily_weight_fragement, container, false);
        return contentView;
    }

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

        btnTime = (EditText) contentView.findViewById(R.id.time_field);
        btnTime.setOnClickListener(this);
        btnDate = (EditText) contentView.findViewById(R.id.date_field);
        btnDate.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){

        switch (v.getId()){

            case R.id.time_field:

                timePickerDialog = new TimePickerDialog(KeyInWeightF.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                        Calendar timeCalendar = Calendar.getInstance();
                        timeCalendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
                        timeCalendar.set(Calendar.MINUTE,minute);

                        String timestring = DateUtils.formatDateTime(KeyInWeightF.this,timeCalendar.getTimeInMillis(),DateUtils.FORMAT_SHOW_TIME);
                                                                     // KeyInWeightF.this error
                        btnTime.setText("Time:"+timestring);



                    }
                },calender.get(Calendar.HOUR_OF_DAY),calender.get(Calendar.MINUTE),android.text.format.DateFormat.is24HourFormat(KeyInWeightF.this));
                                                                                                                                       //KeyInWeightF.this error
                timePickerDialog.show();
                break;

            case R.id.date_field:{

                datePickerDialog =new DatePickerDialog(KeyInWeightF.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                        Calendar dateCalender = Calendar.getInstance();
                        dateCalender.set(Calendar.YEAR,year);
                        dateCalender.set(Calendar.MONTH,monthOfYear);
                        dateCalender.set(Calendar.DAY_OF_MONTH,dayOfMonth);

                        String dateString = DateUtils.formatDateTime(KeyInWeightF.this,dateCalender.getTimeInMillis(),DateUtils.FORMAT_SHOW_TIME);
                                                                     // error of KeyInWeightF.this why ?
                        btnDate.setText("Date:" + dateString);
                    }
                },calender.get(Calendar.YEAR),calender.get(Calendar.MONTH),calender.get(Calendar.DAY_OF_MONTH));

                datePickerDialog.show();
                break;

        }
    }
}

错误Logcat

Error:(65, 54) error: method formatDateTime in class DateUtils cannot be applied to given types;
required: Context,long,int
found: KeyInWeightF,long,int
reason: actual argument KeyInWeightF cannot be converted to Context by method invocation conversion

Error:(72, 114) error: method is24HourFormat in class DateFormat cannot be applied to given types;
required: Context
found: KeyInWeightF
reason: actual argument KeyInWeightF cannot be converted to Context by method invocation conversion

Error:(88, 54) error: method formatDateTime in class DateUtils cannot be applied to given types;
required: Context,long,int
found: KeyInWeightF,long,int
reason: actual argument KeyInWeightF cannot be converted to Context by method invocation conversion

Error:(79, 35) error: no suitable constructor found for DatePickerDialog(KeyInWeightF,<anonymous OnDateSetListener>,int,int,int)
constructor DatePickerDialog.DatePickerDialog(Context,int,OnDateSetListener,int,int,int) is not applicable
(actual and formal argument lists differ in length)
constructor DatePickerDialog.DatePickerDialog(Context,OnDateSetListener,int,int,int) is not applicable
(actual argument KeyInWeightF cannot be converted to Context by method invocation conversion)

我该如何解决?

首先纠正你的onCreateView方法。

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            contentView=inflater.inflate(R.layout.daily_weight_fragement, container, false);
            btnTime = (EditText) contentView.findViewById(R.id.time_field);
            btnTime.setOnClickListener(this);
            btnDate = (EditText) contentView.findViewById(R.id.date_field);
            btnDate.setOnClickListener(this);
            return contentView;
        }

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


        }

Logcat Returns

Error:(79, 35) error: no suitable constructor found for DatePickerDialog(KeyInWeightF,,int,int,int) constructor DatePickerDialog.DatePickerDialog(Context,int,OnDateSetListener,int,int,int) is not applicable (actual and formal argument lists differ in length) constructor DatePickerDialog.DatePickerDialog(Context,OnDateSetListener,int,int,int) is not applicable (actual argument KeyInWeightF cannot be converted to Context by method invocation conversion)

解决方案

你应该使用 getActivity()而不是KeyInWeightF.this

getActivity() 在片段 returns 中 Activity 该片段当前关联。

timePickerDialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {

相同
  String timestring = DateUtils.formatDateTime(getActivity(),timeCalendar.getTimeInMillis(),DateUtils.FORMAT_SHOW_TIME);

要在选项卡片段中创建日期和时间选择器,您可以参考 this

甚至 this 也有帮助。

您应该使用 getActivity() 而不是 SomeActiviyy.this

datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener()