I am trying to use a custom time picker and then run the picker from a fragment. This is the code for the time picker dialog (TimePickerFragment.java):
package com.ravit.android.aroundtheclock;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TimePicker;
import java.util.Calendar;
/**
* A simple {@link Fragment} subclass.
*/
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
public TimePickerFragment() {
// Required empty public constructor
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
@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);
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}
and this is the code from the onCreateView fragment that shows the time picker (WorkdayFragment.java):
mButtonEnd = (Button)v.findViewById(R.id.end_workday);
mButtonEnd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TimePickerFragment dialog = new TimePickerFragment();
dialog.show(getActivity().getFragmentManager(), DIALOG_TIME);
}
});
This is the error I'm getting when pressing the button. It seems like there is a problem loading the view:
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: FATAL EXCEPTION: main
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: Process: com.ravit.android.aroundtheclock, PID: 20325
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at com.android.internal.policy.PhoneWindow.requestFeature(PhoneWindow.java:317)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at com.android.internal.app.AlertController.installContent(AlertController.java:231)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at android.app.AlertDialog.onCreate(AlertDialog.java:423)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at android.app.Dialog.dispatchOnCreate(Dialog.java:394)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at android.app.Dialog.show(Dialog.java:295)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at android.app.DialogFragment.onStart(DialogFragment.java:499)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at android.app.Fragment.performStart(Fragment.java:2244)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1002)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at android.app.BackStackRecord.run(BackStackRecord.java:793)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
My guess is that there is a problem with the way the time picker code is written because when I replace it with date picker then everything runs without errors.
I tried to solve the requestFeature() error according to other answers on this topic with no progress.
Any advice on how to continue from here would be very helpful.
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…