0

I've created a splash screen, and it works pretty fine at first, but after that, it shows me a white blank screen instead of my splash screen image file. I've no idea why that happens.

I tried to change my style.xml parent theme, but some of the themes crash my app, and only Theme.AppCompat.Light.NoActionBar works and gives me a blank white screen.

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();
    }
}

Screen sequence, thread sleep time, and everything else works fine except that the image is not showing.

Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
E Heng
  • 91
  • 3
  • 9

5 Answers5

3

In your onCreate method, you forgot to add setContentView(R.layout.splash);

Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
2

YOU MISSING 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();

    }
}
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
2

You need to add setContentView to your onCreate method.

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();
 }
}
0

You have to set Layout in onCreate method of Splash activity like:

setContentView(R.layout.splash);
Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103
0

instead of using thread and sleep function use Handler , and do some thing like this :

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);
Russell Ghana
  • 2,963
  • 3
  • 20
  • 31