1

This link shows how to create a Time Picker using DialogFragment http://developer.android.com/guide/topics/ui/controls/pickers.html#TimePicker

Is it possible that there is a typo in the example shown? When I copy and paste the example in a new class in Eclipse, it says that static is an illegal modifier. What am I doing wrong? This is what I have in Eclipse:

package com.example.symptommanagement;
import java.util.Calendar;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;

public static class TimePickerFragment extends DialogFragment
        implements TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
    }

}

By the way, if I delete the static modifier, the code works.

Michael
  • 1,834
  • 2
  • 20
  • 33
  • possible duplicate of [Illegal modifier error for static class](http://stackoverflow.com/questions/11831791/illegal-modifier-error-for-static-class) – James McCracken Oct 15 '14 at 23:53

1 Answers1

1

I think you created a simple, seperated class for the Fragment. See this SO question:

Fragments have to be either regular Java classes or static inner classes, and they need to have a public zero-argument constructor.

I think, all the examples on the Google Developer site written to be static inner classes.

In Java only the nested classes can be static classes, see: Why are you not able to declare a class as static in Java?

Community
  • 1
  • 1
Nagy Vilmos
  • 1,878
  • 22
  • 46