6

到目前为止,我从 ActionBarSherlock 迁移到 ActionBarCompat 没有更大的问题。在我的 Nexus 4 上,它运行良好。当我开始在运行 2.3.6 的旧 Nexus One 上进行测试时,我在旋转设备时遇到了问题。我请求一个 Window 功能来显示 ActionBar 中的中间进度:

@Override
protected void onCreate(final Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    requestWindowFeature(android.view.Window.FEATURE_PROGRESS);
    requestWindowFeature(android.view.Window.FEATURE_INDETERMINATE_PROGRESS);

    setupActionBar(getSupportActionBar());

}

这在果冻豆上效果很好。

在 2.3.6 上获取此堆栈跟踪:

08-12 13:34:09.242: E/AndroidRuntime(4602): FATAL EXCEPTION: main
08-12 13:34:09.242: E/AndroidRuntime(4602): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.app.packagename/net.app.fragment.CustomActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
08-12 13:34:09.242: E/AndroidRuntime(4602):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2832)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at android.app.ActivityThread.access$1600(ActivityThread.java:117)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at android.os.Looper.loop(Looper.java:130)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at java.lang.reflect.Method.invokeNative(Native Method)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at java.lang.reflect.Method.invoke(Method.java:507)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at dalvik.system.NativeStart.main(Native Method)
08-12 13:34:09.242: E/AndroidRuntime(4602): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
08-12 13:34:09.242: E/AndroidRuntime(4602):     at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at android.app.Activity.requestWindowFeature(Activity.java:2729)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at net.app.fragment.BaseFragmentActivity.onCreate(BaseFragmentActivity.java:50)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at net.app.fragment.CustomActivity.onCreate(CustomActivity.java:55)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-12 13:34:09.242: E/AndroidRuntime(4602):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-12 13:34:09.242: E/AndroidRuntime(4602):     ... 12 more
08-12 13:34:12.092: I/Process(4602): Sending signal. PID: 4602 SIG: 9

任何人都知道如何解决这个问题?

问候

4

4 回答 4

12

打电话supportRequestWindowFeature(Window.FEATURE_...)

请参阅http://android-developers.blogspot.com/2013/08/actionbarcompat-and-io-2013-app-source.html

于 2013-08-12T11:51:51.303 回答
0

利用supportRequestWindowFeature(WindowCompat.FEATURE_xxx)

即您还需要使用WindowCompat.FEATURE_xxx而不是Window.FEATURE_xxx向后兼容。

于 2014-03-10T09:10:39.683 回答
0

对于那些不调用requestWindowFeature()方法但仍然遇到异常的人,请尝试以下解决方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
    } catch (Throwable e) {
        //dummy workaround
    }
    setContentView(getLayoutResId());

https://code.google.com/p/android/issues/detail?id=60841

于 2014-04-03T13:13:40.957 回答
0

错误日志清楚地表明“必须在添加内容之前调用 requestFeature()”。所以你必须调用'super.onCreate(savedInstanceState);' 在“requestWindowFeature”调用之后。并且不需要如上所述调用 supportRequestWindowFeature(Window.FEATURE_...) 。请参阅下面的工作代码。

  @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        requestWindowFeature(Window.FEATURE_PROGRESS);
        super.onCreate(savedInstanceState);
于 2016-07-14T22:11:54.670 回答