在Tab Fragment错误中创建DatePicker和TimePicker

时间:2015-12-08 09:06:21

标签: android datepicker datetimepicker android-tabs timepicker

我正在尝试在date picker片段中创建number pickertab,但是它会收到错误。由于扩展fragment不同于扩展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)

我该如何解决?

3 个答案:

答案 0 :(得分:1)

首先纠正您的oracle.sql.Blob方法。

onCreateView

Logcat返回

  

错误:(79,35)错误:找不到合适的构造函数   DatePickerDialog(KeyInWeightF ,, int,int,int)构造函数   DatePickerDialog.DatePickerDialog(上下文,INT,OnDateSetListener,INT,INT,INT)   不适用(实际和正式的参数列表长度不同)   构造函数   DatePickerDialog.DatePickerDialog(上下文,OnDateSetListener,INT,INT,INT)   不适用(实际参数KeyInWeightF无法转换为   通过方法调用转换上下文)

解决方案

您应该使用 @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); } 代替getActivity()

片段中的

getActivity() 会返回片段当前与之关联的活动。

KeyInWeightF.this

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

答案 1 :(得分:0)

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

甚至this都很有用。

答案 2 :(得分:0)

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

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