Pantheo
Pantheo

Reputation: 129

Application crashes when it tries to launch Intent

I have a splash screen that i want to run before my main app screen. However, when the timer ends, the applicaiton crashes. Any ideas on why this happens? Thanks in advance.

Below is the referenced code

public class Splash extends Activity {

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

        Thread timer = new Thread() {
            // Whatever is enclosed in the {} of method run(), runs when we
            // start the application
            public void run() {
                try {
                    sleep(2000);

                } catch (InterruptedException e) {
                    e.printStackTrace();

                } finally {

                    Intent openMainScreen = new Intent("com.package.Main_Screen");
                    startActivity(openMainScreen);

                }
            }
        };

        timer.start();
    }
}

Upvotes: 0

Views: 96

Answers (4)

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

Write below code

Intent openMainScreen = new Intent(this, MainActivity.class);
startActivity(openMainScreen);

instead of

Intent openMainScreen = new Intent("com.package.Main_Screen");
startActivity(openMainScreen);

And declare your MainActivity into Androidmanifest.xml file.

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

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

it will solve your problem.

Upvotes: 0

Rasel
Rasel

Reputation: 15477

You are calling startActivity from different Thread.You have to run it from UI thread.What you are trying to achieve can be done easily by

    public class Splash extends Activity {
        Handler handler;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_splash);
            handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    Intent openMainScreen = new Intent(Splash.this,
                            Main_Screen.class);
                    startActivity(openMainScreen);

                }
            }, 2000);
        }
    }

Upvotes: 0

kumar_android
kumar_android

Reputation: 2263

You have to call like this

Intent openMainScreen = new Intent(ClassName.this, MainActivity.class);
startActivity(openMainScreen);

And you have to register it in Manifest file

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >

Upvotes: 0

Andro Selva
Andro Selva

Reputation: 54322

Why don't you simply use this kind of Intent,

Intent openMainScreen = new Intent(Splash.this,Main_Screen.class);
startActivity(openMainScreen);

And also make sure you you have added the Activity in your manifest like this,

<activity android:name=".Main_Screen">
 </activity>

Upvotes: 1

Related Questions