如何在自定义 PreferenceActivity 中显示不确定的进度图标?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是你在 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().