requestFeature() must be called before adding content问题的解决方法

在Android Studio中运行程序,在同时使用requestWindowFeature()和setContentView()时,如果不注意顺序就会爆出如下错误;

android.util.AndroidRuntimeException: requestFeature() must be called before adding content

这是因为两者使用顺序出错的缘故,可以通过字面意思了解就是requestFeature()的使用必须在前面。可以通过报错提示: at com.example.dareway.yofog.Welcome.WelcomeActivity.onCreate(WelcomeActivity.java:24)找到第24行—-requestWindowFeature(Window.FEATURE_NO_TITLE);

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
      }

进行一个简单的调换顺序就可以解决错误:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_welcome);
      }

你可能感兴趣的:(移动开发,文字随笔)