如何在自定义 PreferenceActivity 中显示不确定的进度图标?

发布于 2024-11-05 11:00:44 字数 819 浏览 1 评论 0原文

public class MyPreferences extends PreferenceActivity {

    @Override
    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        requestWindowFeature(Window.FEATURE_PROGRESS);
        ...
        setProgressBarIndeterminateVisibility(true);
        setProgressBarVisibility(true);
    }
}

在第一次 requestWindowFeature() 调用时,会抛出:

    ERROR/AndroidRuntime(16406): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content

显然,此时我还没有添加任何内容。 PreferenceActivity 在 super.onCreate() 期间是否改变了某些状态,导致任何孩子认为它已经改变了?或者我应该使用一些明显的属性?

我还应该指出,我在没有 xml 的情况下创建这些首选项,即完全以编程方式创建,但代码片段足以表明在任何一种情况下都会出现问题。

public class MyPreferences extends PreferenceActivity {

    @Override
    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        requestWindowFeature(Window.FEATURE_PROGRESS);
        ...
        setProgressBarIndeterminateVisibility(true);
        setProgressBarVisibility(true);
    }
}

At the first requestWindowFeature() call, this throws:

    ERROR/AndroidRuntime(16406): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content

Clearly I've not added any content at this point. Has the PreferenceActivity changed some state during the super.onCreate() which causes any children to think it has? Or is there some manifest attribute I should be using?

I should also point out I'm creating these Preferences without xml, i.e. entirely programmatically, but the code snippet is enough to show the problem appears to arise in either case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他夏了夏天 2024-11-12 11:00:44

问题是你在 requestWindowFeature() 之前有 super.onCreate() 。由于这是一个 PreferenceActivity,屏幕上有一些在 onCreate() 中创建的标准项目。

只需将 onCreate() 移至 requestWindowFeature() 下方即可。

The issue is that you have super.onCreate() before requestWindowFeature(). Since this is a PreferenceActivity, there are some standard items on the screen that are created in onCreate().

Simply move onCreate() below requestWindowFeature().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文