Why isn't my splash screen working?

I am new to Java and xml. I am looking for a splash screen to start when I start my application for about 5 seconds. I took the splash screen code from to set it up, but I can't seem to get it to work for some reason, can someone help me! Greetings

MY splash class

   package com.darraghoflaherty.competer.game;

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 {

/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 5000;

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

    /* 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_LENGTH);
}

      

My xml code

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#0099FF">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/ss1"
    android:id="@+id/ss1"
    android:textColor="#ffffff"
    android:textSize="260sp"/>




 </RelativeLayout>

      

+3


source to share


5 answers


first change this world of code:

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

      

For



public void run() {
    /* Create an Intent that will start the Menu-Activity. */
    Intent mainIntent = new Intent(getApplicationContext(),Menu.class);
    startActivity(mainIntent);
    finish();
}

      

Now your code is clear, the error should come from the manifest file. Go to your manifest file and change the position of the intent filter from main activity to slashscreen activity. here's the code:

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

      

+2


source


I am assuming you have not changed the launch activity in the manifest. Android looks in AndroidManifest.xml to select the action to run first. Obviously your manifest contains the following lines:

<activity android:name=".Menu" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

      

This should be changed to:



<activity android:name=".Splash" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".Menu"/>

      

It's also a good convention for denoting actions XyzActivity

, so in your case, MenuActivity

and SplashActivity

.

+1


source


use this code for splash screen ...

public class SPLASH extends Activity {


protected boolean _active = true;
protected int _splashTime = 3000; // time to display the splash screen in MICROSECONDS 


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



    Thread splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                while (_active && (waited < _splashTime)) {
                    sleep(100);
                    if (_active) {
                        waited += 100;
                    }
                }
            } catch (Exception e) {

            } finally {

                startActivity(new Intent(Splash.this,Menu.class));
                finish();
            }
        };
             };
    splashTread.start();

    }

}

      

and make Splash activity a startup activity with

<activity
        android:name=".SPLASH"
        android:label="@string/app_name"
        android:theme="@style/NoActionBar" 
        android:screenOrientation="portrait">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

      

Hope this helps u

0


source


/* New Handler to start the Menu-Activity
 * and close this Splash-Screen after some seconds.*/

        Thread timer = new Thread(){
        public void run(){
            try{
                Thread.sleep(2000);
            }
            catch(InterruptedException e){
                e.printStackTrace();
            }
            finally{
                 Intent mainIntent = new Intent(Splash.this,Menu.class);

                startActivity(mainIntent );

                finish();
            }
        }

    };// end thread
    timer.start();

      

0


source


Hey checkOut, I've gotten a little better. thank

public class Splash extends Activity {

private final int SPLASH_DISPLAY_LENGTH = 5000;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);

   nav();
}

public void nav() {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {


         Intent mainIntent = new Intent(Splash.this,Menu.class);
        startActivity(mainIntent);
       finish();

        }
    }, SPLASH_DISPLAY_LENGTH);
  }

@Override
protected void onPause() {
    super.onPause();
    finish();
}

      

}

-1


source







All Articles