在super.onCreate上添加内容错误之前,必须调用requestFeature()

我有一个扩展ActionBarActivity的抽象类。 在onCreate ,我有:

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

内容错误之前,由于requestFeature(),应用程序崩溃,特别是在super.onCreate(savedInstanceState) 。 阅读了一些类似的post后,我想出了这个解决scheme:

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

我的问题是:为什么它在超级通话中崩溃? 此外,我没有settingContentView的类扩展这个类,直到我打电话super.onCreate 。 它仍然偶尔崩溃。

 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 

Solutions Collecting From Web of "在super.onCreate上添加内容错误之前,必须调用requestFeature()"

因为android.support.v7.app.ActionBarActivity通过添加一个ActionBar来改变窗口的内容。
请看看开始的代码

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

有关详细信息, 请访问https://android.googlesource.com/platform/frameworks/support/+/master/v7/appcompat/src/android/support/v7/app/ActionBarActivity.java
FEATURE_INDETERMINATE_PROGRESS外观取决于是否存在一个ActionBar。 所以需要在超级通话之前设置。