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

12

88

I am getting this android.util.AndroidRuntimeException: requestFeature() must be called before adding content error. As you can see in the below code, the requestWindowFeature(Window.FEATURE_NO_TITLE); line comes before setContentView(R.layout.mainmenu); line of code. This onCreate() code is the same format in just about every one of my activities and I've never had trouble with it before until now. Ever since I updated to ADT 22 a lot of random errors have been popping up everywhere. I have weeded through a lot of those errors and this is my latest one.

What can I do to fix this error?

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

LogCat

05-31 04:20:43.121: E/AndroidRuntime(14559): FATAL EXCEPTION: main
05-31 04:20:43.121: E/AndroidRuntime(14559): java.lang.RuntimeException: Unable to start activity ComponentInfo{matt.lyons.bibletrivia.lite/matt.lyons.bibletrivia.lite.MainMenu}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.os.Looper.loop(Looper.java:137)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread.main(ActivityThread.java:5041)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at java.lang.reflect.Method.invokeNative(Native Method)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at java.lang.reflect.Method.invoke(Method.java:511)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at dalvik.system.NativeStart.main(Native Method)
05-31 04:20:43.121: E/AndroidRuntime(14559): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
05-31 04:20:43.121: E/AndroidRuntime(14559):    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:229)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.Activity.requestWindowFeature(Activity.java:3244)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at matt.lyons.bibletrivia.lite.MainMenu.onCreate(MainMenu.java:28)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.Activity.performCreate(Activity.java:5104)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-31 04:20:43.121: E/AndroidRuntime(14559):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-31 04:20:43.121: E/AndroidRuntime(14559):    ... 11 more
Preciado answered 5/6, 2013 at 12:19 Comment(2)
Can you elaborate? I do not know what "through theme" means.Preciado
You can apply theme in Manifest under activity tag like, android:theme="@android:style/Theme.NoTitleBar.Fullscreen" instead writing java code in onCreate.Procora
P
196

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.
Procora answered 5/6, 2013 at 12:28 Comment(7)
@clu This requestFeature (int featureId) must be called before setContentView(). May be called as many times as desired as long as it is before setContentView(). If not called, no extended features will be available. You can not turn off a feature once it is requested. For more detail click hereProcora
I would advise against calling requestWindowFeature before super.onCreate. You can't be sure about the state of the Activity before that point.Melamie
@vishesh chandra can you help in my case this is not worked for me #27404646Bowlds
Solved the problem, but the title bar still appears.Parulis
Yes, for me also the action bar still appearsAbaddon
I'm also facing the same issue. Even I tried but placing the line before the super.onCreate... My application didn't crash but there was no change. The action bar was still visibleAbaddon
To hide the action bar you can use getActionbar().hide(); or getActivity().getActionbar().hide();.Procora
G
14

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.

Gree answered 12/8, 2014 at 9:48 Comment(3)
Worked also for me. I'm using ActionBarActivity from the support library v21.Kettledrum
I'm using appCompat and so supportRequestWindowFeature did the trick for me and I didn't have to put it before super.onCreateErasmoerasmus
sure this used to work since this post is 5 years old, but just fyi: Window.FEATURE_INDETERMINATE_PROGRESS is deprecated as of api vs. 24Persona
E
7

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()...

Edgeworth answered 26/2, 2015 at 5:12 Comment(0)
O
7

Please check your class is extend from Activity or ActionBarActivity. If you are using ActionBarActivity please use Activity.

Otherworld answered 20/4, 2015 at 14:1 Comment(1)
This was my issue! I was extending AppCompatActivity and the requestWindowFeature(Window.FEATURE_NO_TITLE) was not successful in hiding the title bar until I switched to extending Activity.Kapok
G
3

I also ran into this error from a different workflow. I created a custom DialogFragment class and I created two @Override functions - onCreateView and onCreateDialog. My onCreateView function grabbed a custom layout for the fragment, and my onCreateDialog function created an AlertDialog.Builder.

This appeared to not work because the onCreateDialog is called before onCreateView. After I deleted onCreateView [by moving my custom view inflation into onCreateDialog, i encountered the error:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

I realized my difficulty came from trying to implement both overrides since I wanted to 1) use a layout for the main view of the dialog, and 2) use the Builder pre-defined Positive / Negative buttons. My solution was to create the positive / negative buttons in my custom dialog view, so i deleted my implementation of the Override onCreateDialog function.

Hope this helps someone in the future!

Here are some SO questions that helped me:

Granuloma answered 19/7, 2016 at 18:39 Comment(0)
A
2

I was extending a DialogFragment and the above answer didnot work. I had to use getDialog() to remove the title:

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
Athanasia answered 23/6, 2014 at 10:15 Comment(0)
S
2

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowActionBar">false</item>

    </style>

just only set style like this no any coding side changing is required.

Shelleyshellfire answered 3/2, 2016 at 5:18 Comment(0)
M
1

Extend (Activity) instead of (ActionBarActivity)

example: public class Itemdetails extends Activity {....

then in the onCreate write:

super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.Your_Activity);
Masson answered 5/11, 2015 at 15:10 Comment(0)
S
1

Misplaced your line: super.onCreate(savedInstanceState);

use this Manner:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrscanner);
Season answered 31/7, 2019 at 5:23 Comment(0)
P
1

I had this error with AlertDialog and the error was happening in API 19 only when dialog.show() was called.

figured out that the android.app.AlertDialog import is causing the problem so I changed it to androidx.appcompat.app.AlertDialog and it fixed it.

Note that this will work if you are using androidx. if you are not, you should use the old appcompat version of that import.

Prophetic answered 9/9, 2020 at 13:1 Comment(0)
N
0

i think the simplest way to do this is use this below code

  getActionBar().setTitle(null);
Nagaland answered 19/8, 2016 at 9:15 Comment(0)
P
0

Please try the following options, at least one of these should work for you:

  1. If you are using a DialogFragment then try not overriding both methods onCreateDialog(..) and onCreateView(..) (This change worked for me)

  2. try supportRequestWindowFeature(Window.FEATURE_NO_TITLE)/requestWindowFeature(Window.FEATURE_NO_TITLE) before setContentView(..) in the activity and after super.onCreate(..);

  3. Try option 2 before super.onCreate(..);

Planchet answered 15/2, 2019 at 18:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.