2

This particular error message is popular on SO and the web but I still can't find a solution from any of the suggested ones to work for me.

Am trying to add a DatePicker to my fragment, which gets fired when a button is clicked.But this error keeps popping up. This is the Fragment class am using:

public class MyFragment extends DialogFragment implements 
                     DatePickerDialog.OnDateSetListener {

Context context;


@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {


    //Use the current date as the default value for the picker
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);

    //Create a new instance of datepicker and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);

}
public void onDateSet(DatePicker view, int year, int month, int day) {


}



public View onCreateView(LayoutInflater inflater, ViewGroup container, 

                                 Bundle savedInstanceState) {


    View rootView = inflater.inflate(R.layout.my_layout, container, false);

    Button reminderButton = (Button) rootView.findViewById(R.id.dueDateButton);
    reminderButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment dialogFragment = new MyFragment();
            dialogFragment.show(getFragmentManager(), "datePicker");
        }
    });

    context = getActivity(); 


    return rootView;
}

}

This Fragment handles one of the views in a tabbed activity. I have tried adding:

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

just before:

View rootView = inflater.inflate(R.layout.newdebt_layout, container, false);

with no luck. I have also tried adding this:

getWindow().requestFeature(Window.FEATURE_NO_TITLE);

right before the setContentView() method in the activity where this fragment is embedded with no luck also.

I have looked at this, this and this questions and a host of others with no luck.

In the stack trace, no specific line in the code is flagged.

05-27 06:17:30.687  23204-23204/com.ojonugwaochalifu.blablabla E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ojonugwaochalifu.blablabla, PID: 23204
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
        at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:336)
        at com.android.internal.app.AlertController.installContent(AlertController.java:242)
        at android.app.AlertDialog.onCreate(AlertDialog.java:337)
        at android.app.Dialog.dispatchOnCreate(Dialog.java:374)
        at android.app.Dialog.show(Dialog.java:267)
        at android.support.v4.app.DialogFragment.onStart(DialogFragment.java:399)
        at android.support.v4.app.Fragment.performStart(Fragment.java:1813)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:989)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5867)
        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:858)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
        at dalvik.system.NativeStart.main(Native Method)
Community
  • 1
  • 1
Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132

1 Answers1

0

I faced the same problem. I was inflating another custom dialog view in the onCreateView method. Removing that solved the problem.

The onCreateView method is called each time show is invoked. Make sure you aren't unconditionally inflating any other view in onCreateView.

H. Saxena
  • 335
  • 4
  • 11