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.

I got that exception (android.util.AndroidRuntimeException: requestFeature() must be called before adding content) when using

requestWindowFeature(Window.FEATURE_NO_TITLE);

in an older device running Android 2.3.5 (Gingerbread). I am using the v7 support library.

The error was fixed when I changed it to use:

supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

(This comes after my super.onCreate call in the fix, too). See docs at https://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html#supportRequestWindowFeature(int)

So it may be more a case of a misleading error message than anything else.


If you're using your activity as a Dialog (with Theme.Dialog), then make sure you extend Activity instead of ActionBarActivity (the default given to you with the Android Studio wizard). Then you can use

requestWindowFeature(Window.FEATURE_NO_TITLE);

or

supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

AFTER the super.onCreate()...

Tags:

Java

Android