为什么我将全屏设置为我的活动android时会出现错误?

时间:2015-06-29 10:44:02

标签: android

我将此全景代码写入我的活动:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);
        ImageView img=(ImageView)findViewById(R.id.imageView1);
        img.setImageResource(R.drawable.tologo);
        getWindow().getDecorView().setBackgroundColor(Color.WHITE);

    }

但是当我运行我的应用程序时,我收到此错误:

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


我从这个链接写下代码:
TUTORIAL

2 个答案:

答案 0 :(得分:1)

这是我的解决方案:

<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light">
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

链接Full Screen Theme for AppCompat

答案 1 :(得分:0)

将您的onCreate()方法更改为此内容。

@Override
protected void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().getDecorView().setBackgroundColor(Color.WHITE);

    ImageView img = (ImageView) findViewById(R.id.imageView1);
    img.setImageResource(R.drawable.ic_launcher);

    setContentView(R.layout.activity_main);

}