启动画面上的白色

White color on Splash Screen

我创建了启动画面,一开始它工作得很好,但之后,它向我显示了一个白色的空白屏幕,而不是我的启动画面图像文件。我不知道为什么会这样。

我试图更改我的 style.xml 父主题,但一些主题使我的应用程序崩溃,只有 Theme.AppCompat.Light.NoActionBar 有效并给我一个空白的白屏。

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

Splash.java

public class Splash extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Thread ssThread = new Thread(){
            @Override
            public void run() {
                try {
                    sleep(3000);
                    Intent startMainScreen = new Intent(getApplicationContext(),MainActivity.class);
                    startActivity(startMainScreen);
                        finish();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        ssThread.start();
    }
}

屏幕序列、线程休眠时间以及除图像未显示外其他一切正常。

你错过了 setContentView(R.layout.YOUR_XML_NAME);

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xxxx);


         /****** Create Thread that will sleep for 3 seconds *************/        
        Thread background = new Thread() {
            public void run() {

                try {
                    // Thread will sleep for 3 seconds
                    sleep(3*1000);

                    // After 5 seconds redirect to another intent
                    Intent startMainScreen = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(startMainScreen);

                    //Remove activity
                    finish();

                } catch (Exception e) {

                }
            }
        };

        // start thread
        background.start();
}

    @Override
    protected void onDestroy() {

        super.onDestroy();

    }
}

在您的 onCreate 方法中,您忘记添加 setContentView(R.layout.splash);

您必须在 onCreate Splash activity 方法中设置布局,例如:

setContentView(R.layout.splash);

使用 Handler 而不是使用线程和睡眠函数,并做一些这样的事情:

setContentView(R.layout.splash_screen);
int interval = 3000; // 3 second
new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            Intent intent = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }, interval);

您需要将 setContentView 添加到您的 onCreate 方法中。

public class Splash extends AppCompatActivity {
@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  /*
   *add setContentView here after super.onCreate( )
   */
  setContentView( R.layout.splash_layout);
  Thread ssThread = new Thread(){
   @Override
    public void run() {
     try {
      sleep(3000);
      Intent startMainScreen = new Intent(getApplicationContext(),MainActivity.class);
      startActivity(startMainScreen);
      finish();
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
    }
   };
   ssThread.start();
 }
}