在选项卡片段错误中创建一个DatePicker和TimePicker [英] Create a DatePicker and TimePicker in the Tab Fragment error

查看:125
本文介绍了在选项卡片段错误中创建一个DatePicker和TimePicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在tab片段内创建date pickernumber picker,但是它得到了错误.我无法弄清错误,或者因为扩展fragment与扩展AppCompatActivity ??

I am trying to create date picker and number picker inside a tab fragment, but its getting error. I can't figure it out the error or I am doing at a wrong way because of extends fragment is different than the extends AppCompatActivity??

有人可以指出我想念的行会吗?

Can someone please point out what am I missing and guild ?

 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返回

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

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() 在片段中返回该片段当前与之关联的活动.

getActivity() in a Fragment returns the Activity the Fragment is currently associated with.

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

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

这篇关于在选项卡片段错误中创建一个DatePicker和TimePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆