-3

Initially, the first version of my app worked fine. Later, after changing to the sidebar drawer application template, it does not function properly, crashing the application (due to the splash screen). What could be the issue?

This is the code for the SpashScreen.java file:

public class SplashScreen extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    Thread myThread = new Thread(){
        @Override
        public void run(){
            try {
                sleep(3000);
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    myThread.start();
}

}

This is the manifest:

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/example"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".SplashScreen"
        android:label="@string/example"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity"></activity>
</application>
Jack D.
  • 31
  • 6

4 Answers4

-1

Try this might help

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }, 3000);
akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
-1

Try to use :

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(SplashScreen.this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }, 3000);

This code don't create new Thread, P.S. Android cannot start activity from another Thread (only form UI)

Vova
  • 956
  • 8
  • 22
-1

startActivity() need to be run in main thread. Using postDelay() of Hanlder class

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(i);
            finish();
        }
    }, 3000);
phatnhse
  • 3,870
  • 2
  • 20
  • 29
  • so I should put this fragment of code in my MainActivity? – Jack D. Jul 21 '17 at 14:18
  • In your onCreate method. Read this for understanding how to work with process and thead:https://developer.android.com/guide/components/processes-and-threads.html?hl=vi – phatnhse Jul 21 '17 at 14:19
  • Thank you very much for the link. I put the code in the onCreate method but now it crashes before even showing the splash screen, thank you anyway – Jack D. Jul 21 '17 at 14:23
  • I don't know. I am going to study and understand better how threads work so I can find a soloution. The weird thing for me is that the code I initially wrote worked just fine for the empty activity project, but doesn't work for the drawer side bar project, which has the same code basically... – Jack D. Jul 21 '17 at 14:33
  • Both are activities. Not make sense – phatnhse Jul 21 '17 at 14:34
-1

using handler can solve this do below code it will work for you

  handler.postDelayed(
            new Runnable() {
                public void run() {
                    Intent intent = new Intent(youractivity.this, MainActivity.class);
            startActivity(intent);
                }
            }, 3000);
Deep Naik
  • 491
  • 1
  • 3
  • 10