Requestfeature() Must Be Called Before Adding Content

requestFeature() must be called before adding content

Well, just do what the error message tells you.

Don't call setContentView() before requestFeature().

Note:

As said in comments, for both ActionBarSherlock and AppCompat library, it's necessary to call requestFeature() before super.onCreate()

android.util.AndroidRuntimeException: requestFeature() must be called before adding content

I also faced this problem but when i call window request before calling super.onCreate() then problem was solved, please try it also like..

@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
}

Hope this will help you...:)


Edited: For other possible solutions for Android'd new versions

Hide the Status Bar on Android 4.0 and Lower

<application
...
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
...
</application>

The advantages of using an activity theme are as follows:

  • It's easier to maintain and less error-prone than setting a flag programmatically.
  • It results in smoother UI transitions, because the system has the information it needs to render your UI before instantiating your app's main activity.

Android version is lower than Jellybean

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If the Android version is lower than Jellybean, use this call to hide
// the status bar.
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(R.layout.activity_main);
}

Hide the Status Bar on Android 4.1 and Higher

View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();

Note the following:

  • Once UI flags have been cleared (for example, by navigating away from the activity), your app needs to reset them if you want to hide the bars again. See Responding to UI Visibility Changes for a discussion of how to listen for UI visibility changes so that your app can respond accordingly.
  • Where you set the UI flags makes a difference. If you hide the system bars in your activity's onCreate() method and the user presses Home, the system bars will reappear. When the user reopens the activity, onCreate() won't get called, so the system bars will remain visible. If you want system UI changes to persist as the user navigates in and out of your activity, set UI flags in onResume() or onWindowFocusChanged().
  • The method setSystemUiVisibility() only has an effect if the view you call it from is visible.
  • Navigating away from the view causes flags set with setSystemUiVisibility() to be cleared.

Error : requestFeature() must be called before adding content

requestFeature() must be called before adding content

Well, just do what the error message tells you.

Don't call setContentView() before requestFeature().

Note:

As said in comments, for both ActionBarSherlock and AppCompat library, it's necessary to call requestFeature() before super.onCreate()

How to solve requestFeature() must be called before adding content in android

Problem is caused by MaterialDesignLibrary.due to following line in Dialog class:

@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
....
}

here requestWindowFeature is called after calling setContentView method in Activity.

To fix this issue you should remove requestWindowFeature(Window.FEATURE_NO_TITLE); line from Dialog class of library

AndroidRuntimeException:requestFeature() must be called before adding content in DialogFragment

The problem is that you mash together too many different things and that you don't respect the lifecycle of each class.

Firstly you need to stop nesting classes like you do. This is partly the source of the error. If you really want/have to nest a Fragment or Dialog then it is important that you declare the nested Fragments, Dialogs as static. If you don't declare them as static you can cause memory leaks and problems like yours. But the best thing you can do is to only limit yourself to one class per file unless you have an actual reason to nest it. This also has the added benefit of improving readability and maintainability of your code.

But the main cause of your error is that you completely ignore the lifecycle of the Dialog. Pretty much all of the following code should be in the proper lifecycle methods of the Dialog:

mDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

view = getActivity().getLayoutInflater().inflate(R.layout.maps_dialog, null);

//Line below throws exception. Needs to be commented out
setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Holo);
mDialog.setContentView(view);

So to fix your error you need to do 3 things:

  1. Either declare MapDialogFragmentv2 and MyDialog static like this:

    public class MainActivity extends Activity {

    ...

    public static class MapDialogFragmentv2 extends Fragment {

    ...

    public static class MyDialog extends Dialog {
    ...
    }
    }
    }

Or even better move them to their own files all together.


  1. Move the code from onCreateDialog() in MapDialogFragmentv2 in the correct lifecycle methods in MyDialog. It should then look something like this:

    public static class MyDialog extends Dialog {

    private final LayoutInflater mInflater;

    public MyDialog(Context context) {
    super(context);

    mInflater = LayoutInflater.from(context);
    }

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

    getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    getWindow().getDecorView().setSystemUiVisibility(MainActivity.getImmersiveModeFlags());

    View view = mInflater.inflate(R.layout.maps_dialog, null);
    setContentView(view);
    }

    @Override
    public void show() {
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    super.show();
    }
    }
  2. Add setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Holo); to the onCreate() method of your MapDialogFragmentv2 after the super.onCreate() call.

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

    setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Holo);
    }

Your MapDialogFragmentv2 should then just look like this:

public static class MapDialogFragmentv2 extends DialogFragment {

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

setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Holo);
}

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
return new MyDialog(getActivity());
}
}

I tested everything on my Nexus 5 running Android 5.0.1 (Lollipop) and it works.

android.util.AndroidRuntimeException: requestFeature() must be called before adding content when showing DialogFragment

I found the issue, overriding both onCreateView and onCreateDialog caused that issue. I simply removed onCreateView and evertything works fine now.



Related Topics



Leave a reply



Submit