Android时间选择器对话框

时间:2015-06-21 00:15:18

标签: android android-timepicker

我正在尝试制作时间选择器对话框。我跟着this android documentation。我想在用户单击按钮时显示对话框。当我点击按钮时,我收到此错误“必须在添加内容之前调用requestFeature()。”

这是我的时间选择器片段代码:

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
    }
};

我的按钮位于另一个活动中,其xml“onclick”属性调用此方法:

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

Android告诉我在某处添加requestFeature()。我不明白在哪里放置此代码以及请求哪个功能。

编辑:

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 个答案:

没有答案