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

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

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

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

 @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 

因为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是否存在。 所以需要在超级调用之前设置。