Android Time Picker Dialog

358 views Asked by At

I am trying to make a time picker dialog. I followed this android documentation. I want to show the dialog when the user clicks a button. When I click the button, I get this error "requestFeature() must be called before adding content".

Here is my Time Picker Fragment Code:

public class TimePickerFragment extends DialogFragment {

private OnFragmentInteractionListener mListener;

public static TimePickerFragment newInstance(String param1, String param2) {
    TimePickerFragment fragment = new TimePickerFragment();
    Bundle args = new Bundle();
    fragment.setArguments(args);
    return fragment;
}

public TimePickerFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_time_picker, container, false);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Calendar calendar = Calendar.getInstance();
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);

    TimePickerDialog dialog = new TimePickerDialog(getActivity(), listener, hour, minute, false);
    return dialog;
}

TimePickerDialog.OnTimeSetListener listener =  new TimePickerDialog.OnTimeSetListener(){
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // do something with user's input
    }
};

My button is in another activity and its xml "onclick" attribute calls this method:

public void showTimePickerDialog(View view) {
    DialogFragment timePicker = new TimePickerFragment();
    timePicker.show(getFragmentManager(), "timePicker");
}

Android is telling me to add requestFeature() somewhere. I don't understand where to put this code and which feature to request.

EDIT:

android.util.AndroidRuntimeException: requestFeature() must be called before adding content
        at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:249)
        at com.android.internal.app.AlertController.installContent(AlertController.java:234)
        at android.app.AlertDialog.onCreate(AlertDialog.java:337)
        at android.app.Dialog.dispatchOnCreate(Dialog.java:361)
        at android.app.Dialog.show(Dialog.java:262)
        at android.app.DialogFragment.onStart(DialogFragment.java:490)
        at android.app.Fragment.performStart(Fragment.java:1724)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:918)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
        at android.app.BackStackRecord.run(BackStackRecord.java:684)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
0

There are 0 answers