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
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
or
AFTER the
super.onCreate()
...just only set style like this no any coding side changing is required.
I also faced this problem but when i call window request before calling super.onCreate() then problem was solved, please try it also like..
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
The advantages of using an activity theme are as follows:
Android version is lower than Jellybean
Hide the Status Bar on Android 4.1 and Higher
Note the following:
Extend (Activity) instead of (ActionBarActivity)
example:
public class Itemdetails extends Activity {....
then in the onCreate write:
Please check your class is extend from Activity or ActionBarActivity. If you are using ActionBarActivity please use Activity.