Splash Screen will not display

2019-07-20 06:35发布

I am trying to implement a Splash Screen using this java and xml code. I created a separate java class from my main activity and placed the java code inside. I created an xml layout in my layout file and placed the xml code inside. However, my app appears normally without a splash screen. It never shows, but the app does not have any errors. Eclipse is not showing any errors either. What could be the cause to this? Thank you in advance. Here is the code.

Java:

package com.carouseldemo.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;

public class Splash extends Activity {

    private final int SPLASH_DISPLAY_LENGHT = 1000;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.splash);

        /* New Handler to start the Menu-Activity 
         * and close this Splash-Screen after some seconds.*/
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                /* Create an Intent that will start the Menu-Activity. */
                Intent mainIntent = new Intent(Splash.this,Menu.class);
                Splash.this.startActivity(mainIntent);
                Splash.this.finish();
            }
        }, SPLASH_DISPLAY_LENGHT);
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ImageView android:id="@+id/splashscreen" android:layout_width="wrap_content"
                android:layout_height="fill_parent" android:src="@drawable/cat"
                android:layout_gravity="center"/>
        <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:text="Hello World, splash"/>
</LinearLayout>

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.carouseldemo.main"
      android:versionCode="1"
      android:versionName="1.0"
      >

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" android:screenOrientation="portrait"/>
    <application android:icon="@drawable/buttonone" android:label="@string/app_name">

        <activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest> 

7条回答
smile是对你的礼貌
2楼-- · 2019-07-20 06:50

MainActivity is launcher activity in manifest file. So, splash is not showing.

Change the launcher activity MainActivity to Splash and write another for MainActivity.

<application android:icon="@drawable/buttonone" android:label="@string/app_name">

        <activity android:name=".Splash" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity" />
    </application>
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-07-20 06:50

Use this code, it might help you.

public class SplashScreenActivity extends Activity {

    protected int _splashTime = 2000;

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

        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    sleep(_splashTime);
                }
            } catch (InterruptedException e) {
                // do nothing
            } finally {
                startActivity(new Intent(SplashScreenActivity.this,
                        MainActivity.class));
                SplashScreenActivity.this.finish();
            }
        }
    };
    splashTread.start();
}

Hope it will help you.

查看更多
成全新的幸福
4楼-- · 2019-07-20 06:52

Another way to achieve using CountDownTimer

public class MyCounter extends CountDownTimer {

        public MyCounter(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {

            startActivity(new Intent(SplashScreen.this,
                    TabSample.class));
            finish();

            MCObject.cancel();



        }

        @Override
        public void onTick(long millisUntilFinished) {

        }

    }

Within OnCreate

     MyCounter MCObject;
    MCObject = new MyCounter(3000, 100);
    MCObject.start();
查看更多
劳资没心,怎么记你
5楼-- · 2019-07-20 06:55

Replace your handler

new Thread(new Runnable() {
    public void run() {
        try {
            Thread.sleep(SPLASH_TIME);
            startActivity(intent);
            finish();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}).start();
查看更多
劳资没心,怎么记你
6楼-- · 2019-07-20 06:56

Try out as below :

 public class Splash extends Activity 
  {
   private final int SPLASH_DISPLAY_LENGHT = 1000;  
       @Override
         public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
               setContentView(R.layout.splash);
               new Thread(){
        public void run()
        {
            try
            {
                sleep(SPLASH_DISPLAY_LENGHT);
               Intent mainIntent = new Intent(Splash.this,Menu.class);
                         Splash.this.startActivity(mainIntent);
                        Splash.this.finish();
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        };          
    }.start();
}

EDITED:

  <activity android:name=".Splash" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-07-20 07:03

Replace the Handler part with this :

Thread splashThread = new Thread() {
    public void run() {
                try {
                Thread.sleep(SPLASH_DISPLAY_LENGHT);
                } catch (Exception e) {
                } finally {
                Intent i = new Intent(Splash.this,Menu.class);
                startActivity(i);
                finish();
                }
            }
        };
    splashThread.start();
查看更多
登录 后发表回答