简体   繁体   中英

How to set up splash screen

With Android Studio I want to put in my app, a splash screen loading for 6 seconds before opening the activity of the game. I put both the java code and xml but when I start the device, the splash screen does not appear. I do not understand what is the error. Can you help me?`

This is my SplashScreenActivity.java

public class SplashScreenActivity extends Activity {

private final int SPLASH_DISPLAY_LENGHT = 6000;

/** 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(SplashScreenActivity.this,Menu.class);
            SplashScreenActivity.this.startActivity(mainIntent);
            SplashScreenActivity.this.finish();
        }
    }, SPLASH_DISPLAY_LENGHT);
}

}

And the file xml :

<RelativeLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:background="#ff000000">
<TextView android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:textStyle="bold"
    android:textColor="#fff"
    android:text="LOADING..."
    android:layout_marginTop="250dp"
    android:layout_marginLeft="30dp"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="500dp"
    android:textColor="#ffffffff"
    android:text=""
    android:gravity="center"/>

Seems like your activity isn't selected as a launcher activity. Add this to your android manifest:

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

inside activity tag so it should look like

    <activity
        android:name="your.package.SplashScreenActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

and remove this part from your MainActivity activity tag.

Also it seeems like you have a typo. If you want to launch a MainActivity, you should change your from

  Intent mainIntent = new Intent(SplashScreenActivity.this,Menu.class);

to

  Intent mainIntent = new Intent(MainActivity.this,Menu.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM