0

In my app I have two editText, one for date and another one for time picker. Date picker is working perfectly but time picker throws an error.

Here is my code:

    EditText searchDate;
    EditText searchTime;
    DatePickerDialog searchDatePickerDialog;
    TimePickerDialog timePickerDialog;
    SimpleDateFormat timeFormatter;
    SimpleDateFormat dateFormatter;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.search, container,false);


    searchDate = (EditText)view.findViewById(R.id.search_date);
    searchDate.setInputType(InputType.TYPE_NULL);
    searchTime = (EditText)view.findViewById(R.id.search_time);
    searchTime.setInputType(InputType.TYPE_NULL);
  
  searchDate.setOnClickListener(this);
        searchTime.setOnClickListener(this);

        dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        searchDate.setText(dateFormatter.format(new Date()));

        timeFormatter = new SimpleDateFormat("HH:mm:ss",Locale.US);
        searchTime.setText(timeFormatter.format(new Date()));

        Calendar newCalendar = Calendar.getInstance();


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

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

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

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

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Calendar c = Calendar.getInstance();
                c.set(hourOfDay,minute);
                searchTime.setText(timeFormatter.format(c.getTime()));
            }
        },newCalendar.get(Calendar.HOUR_OF_DAY),newCalendar.get(Calendar.MINUTE));

        return view;
    }

    @Override
    public void onClick(View v) {
        if (v == searchDate) {
            searchDatePickerDialog.show();
        }
        else if (v == searchTime){
            timePickerDialog.show();
        }
    }

And that's the error I obtain:

cannot resolve constructor 'TimePickerDialog( android.support.v4.app.FragmentActivity,anonymous android.app.TimePickerDialog.OnTimeSetListener,int,int)'

Cleb
  • 25,102
  • 20
  • 116
  • 151
Razul
  • 99
  • 1
  • 12
  • this will be helpful http://stackoverflow.com/a/6711000/3156621 – Darshil Shah Aug 12 '15 at 11:49
  • It seems like the constructor you using for timepicker is not valid...you getting this error at compile time or runtime? – EEJ Aug 12 '15 at 11:51
  • What i do? Please fix – Razul Aug 12 '15 at 11:53
  • This is the constructor declaration. `public TimePickerDialog(Context context, android.app.TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView)` .. you may have to add a boolean value as the last parameter – Emil Aug 12 '15 at 11:58
  • Anyone edit my code and post – Razul Aug 12 '15 at 12:00
  • You cant expect anyone else to do the work for you. please try out the suggested comments and reply what was the outcome.. – Emil Aug 12 '15 at 12:04
  • Yeah, Now its working but when i choose the time in picker its throw error like java.lang.ArrayIndexOutOfBoundsException: length=17; index=19 at java.util.Calendar.set(Calendar.java:1122) at com.h2o.SearchFragment$3.onTimeSet(SearchFragment.java:124) in this line c.set(hourOfDay,minute); – Razul Aug 12 '15 at 12:47

1 Answers1

0

As @Boss said, you need to add a boolean value as last parameter. Posting the code to close the question. Credits for him.

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

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Calendar c = Calendar.getInstance();
                c.set(Calendar.HOUR_OF_DAY, hourOfDay);
                c.set(Calendar.MINUTE, minute);
                searchTime.setText(timeFormatter.format(c.getTime()));
            }
        },newCalendar.get(Calendar.HOUR_OF_DAY),newCalendar.get(Calendar.MINUTE), true);
  • Yeah, Now its working but when i choose the time in picker its throw error like java.lang.ArrayIndexOutOfBoundsException: length=17; index=19 at java.util.Calendar.set(Calendar.java:1122) at com.h2o.SearchFragment$3.onTimeSet(SearchFragment.java:124) in this line newDate.set(hourOfDay,minute); – Razul Aug 12 '15 at 12:48
  • Yeah, Im happy that worked. You can close the question if you want. Cya! – gersonmdesouza Aug 12 '15 at 13:17
  • @Appu Please accept the answer if it helped to resolve your issue. – Emil Aug 16 '15 at 15:44