29

I have an abstract class extending ActionBarActivity. In the onCreate, I have:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    ...
}

The app crashes due to the requestFeature() before content error, specifically on the line super.onCreate(savedInstanceState). After reading some of the similar posts, I came up with this solution:

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

My question is: Why does it crash on the super call? Also, I'm not settingContentView in the classes that extend this class until AFTER I call super.onCreate. It's still occasionally crashing.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.cycle.Cycle}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$600(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5454)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)

Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:320)
at android.app.Activity.requestWindowFeature(Activity.java:3283)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:63)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
at com.myapp.core.activity.MyActivity.onCreate(MyActivity.java:83)
at com.myapp.cycle.Cycle.onCreate(Cycle.java:55)
at android.app.Activity.performCreate(Activity.java:5066)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
... 11 more
ono
  • 2,984
  • 9
  • 43
  • 85
  • Reproduce the problem and post your logcat – Squonk Apr 21 '14 at 20:36
  • The activity Cycle extends MyActivity – ono Apr 21 '14 at 20:44
  • may be an internal hidden call to window.getDecorView? It would also lock the features:http://developer.android.com/reference/android/view/Window.html#getDecorView() – rupps Apr 21 '14 at 21:44
  • Probably what @rupps said. I don't know what ActionBarActivity does under the hood, but ActionBarSherlock would basically wrap your content view with a layout that contained the action bar components, so I would imagine ActionBarActivity does something similar. – Karakuri Apr 21 '14 at 22:25
  • I don't call `getDecorView()`. I can't seem to reproduce it which isn't helping me find a solution. – ono Apr 23 '14 at 16:28

1 Answers1

23

Because android.support.v7.app.ActionBarActivity alters the window content by adding an ActionBar.
Please take a look at the code starting with

@Override
protected void onCreate(Bundle savedInstanceState) {
    mImpl = ActionBarActivityDelegate.createDelegate(this);
    super.onCreate(savedInstanceState);
    mImpl.onCreate(savedInstanceState);
}

and on at https://android.googlesource.com/platform/frameworks/support/+/master/v7/appcompat/src/android/support/v7/app/ActionBarActivity.java for details.
And what the FEATURE_INDETERMINATE_PROGRESS looks like depends on whether an ActionBar is present or not. So that needs to be set before the super call.

yanchenko
  • 56,576
  • 33
  • 147
  • 165
  • So `requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);` needs to be added before the super.onCreate in your answer? – ono Apr 27 '14 at 22:30
  • 2
    Yes, requestWindowFeature always has to be called first. – yanchenko Apr 27 '14 at 22:32
  • can you help in my case this is not worked for me http://stackoverflow.com/questions/27404146/when-orientation-changed-i-got-requestfeature-must-be-called-before-adding-con – Sasha Dec 10 '14 at 15:07
  • 1
    @yanchenko whats mlmpl here? – Sasha Dec 23 '14 at 06:12