启动画面上的白色

时间:2015-08-21 11:40:28

标签: android android-activity splash-screen oncreate setcontentview

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

我尝试更改我的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();
    }
}

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

5 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

你错过 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();

    }
}

答案 2 :(得分:2)

您需要将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();
 }
}

答案 3 :(得分:0)

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

setContentView(R.layout.splash);

答案 4 :(得分:0)

而不是使用线程和睡眠功能使用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);