首页 > 技术文章 > android.util.AndroidRuntimeException: requestFeature() must be called before adding content 错误解决方法

daniel-zhang 2016-01-20 16:06 原文

Activity全屏,网上的代码如下:
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
    setContentView(R.layout.activity_main);
}

但在AS中,会提示错误:
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
解决如下:
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

推荐阅读