安卓开发requestWindowFeature问题

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
setProgressBarVisibility(true);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
setTitle("Test Activity");
... ...
}
运行时报错:
01-29 14:40:33.258: E/AndroidRuntime(19750): FATAL EXCEPTION: main
01-29 14:40:33.258: E/AndroidRuntime(19750): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.webviewtest/com.example.webviewtest.MainActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
01-29 14:40:33.258: E/AndroidRuntime(19750): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2391)
01-29 14:40:33.258: E/AndroidRuntime(19750): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2443)

是为什么呢?
如果写成:
requestWindowFeature(Window.FEATURE_PROGRESS);
super.onCreate(savedInstanceState);
就不报错,但是进度条线显示不出来,又是哪里有问题呢?
谢谢!

2个回答

requestFeature() must be called before adding content
代码位置不对,把它放到setcontentview之前

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
}
是这样吗?
运行时报错啊:
01-29 15:18:57.452: E/AndroidRuntime(21818): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content

求指点,求举例

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问