当我使用Fragment时,DatePicker不起作用

时间:2016-09-16 07:25:52

标签: android datepicker fragment

我设置DatePicker它在Activity中工作,当我使用Fragment时它会生成像

这样的错误
Error:(63, 61) error: incompatible types: MainActivity cannot be converted to Context 

这里我粘贴完整的代码。

MainActivity.Java

    package com.example.sachin.datepicker;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;

import java.util.Calendar;
import java.util.Locale;

public class MainActivity extends Fragment implements View.OnClickListener {
        private EditText fromDateEtxt;
        private EditText toDateEtxt;
        private EditText toDateEtxt1;
        private DatePickerDialog fromDatePickerDialog;
        private DatePickerDialog toDatePickerDialog;
        private DatePickerDialog toDatePickerDialog1;
        private SimpleDateFormat dateFormatter;

        View view;
        @TargetApi(Build.VERSION_CODES.N)
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

                View view = inflater.inflate(R.layout.activity_add_wod, container, false);
                dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);

                findViewsById();

                setDateTimeField();
                return view;
        }

        private void findViewsById() {
                fromDateEtxt = (EditText) view.findViewById(R.id.openingdate);
                fromDateEtxt.setInputType(InputType.TYPE_NULL);
                fromDateEtxt.requestFocus();

                toDateEtxt = (EditText) view.findViewById(R.id.Birthdate);
                toDateEtxt.setInputType(InputType.TYPE_NULL);
                toDateEtxt.requestFocus();
                toDateEtxt1 = (EditText) view.findViewById(R.id.Anniversary);
                toDateEtxt1.setInputType(InputType.TYPE_NULL);
        }

        private void setDateTimeField() {
                fromDateEtxt.setOnClickListener(this);
                toDateEtxt.setOnClickListener(this);
                toDateEtxt1.setOnClickListener(this);
                Calendar newCalendar = Calendar.getInstance();


                fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                Calendar newDate = Calendar.getInstance();
                                newDate.set(year, monthOfYear, dayOfMonth);
                                fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
                        }

                },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

                toDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

                        @TargetApi(Build.VERSION_CODES.N)
                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                Calendar newDate = Calendar.getInstance();
                                newDate.set(year, monthOfYear, dayOfMonth);
                                toDateEtxt.setText(dateFormatter.format(newDate.getTime()));
                        }

                },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
                toDatePickerDialog1 = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

                        @TargetApi(Build.VERSION_CODES.N)
                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                Calendar newDate = Calendar.getInstance();
                                newDate.set(year, monthOfYear, dayOfMonth);
                                toDateEtxt1.setText(dateFormatter.format(newDate.getTime()));
                        }

                },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
        }



        @Override
        public void onClick(View view) {
                if(view == fromDateEtxt) {
                        fromDatePickerDialog.show();
                } else if(view == toDateEtxt) {
                        toDatePickerDialog.show();
                }  else if(view == toDateEtxt1) {
                                toDatePickerDialog1.show();
                }
        }
}

此代码无效: -

 fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            Calendar newDate = Calendar.getInstance();
                            newDate.set(year, monthOfYear, dayOfMonth);
                            fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
                    }

            },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

我试了但是它一次又一次地产生同样的错误..

2 个答案:

答案 0 :(得分:1)

您扩展了片段,因此将this替换为getActivity()

fromDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {}

答案 1 :(得分:0)

DatePickerDialog的构造函数需要一个Context参数。

您已将Fragment个实例传递给DatePickerDialog的构造函数,但Fragment不是Context子类。

通过getContext()getActivity()代替this

更改以下行:

fromDatePickerDialog = new DatePickerDialog(this,
    new DatePickerDialog.OnDateSetListener() {

对此:

fromDatePickerDialog = new DatePickerDialog(getContext(),
    new DatePickerDialog.OnDateSetListener() {

或者对此:

fromDatePickerDialog = new DatePickerDialog(getActivity(),
    new DatePickerDialog.OnDateSetListener() {

根据您的代码,您有多个DatePickerDialog,因此您需要对所有代码执行此更改。

P.S。不应该调用Fragment Main Activity