skip to Main Content

This is for a splash screen. I’ve followed the tutorial but it’s still not working. It keeps on getting an error.

This is my code:

package id.ac.umn.finalproject;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class MainActivity extends AppCompatActivity {

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

        Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class);

        new Handler().postDelayed(startActivity(startApp), 3000);
    }
}

2

Answers


  1. In Kotlin try this:

    Handler(Looper.getMainLooper()).postDelayed({
               Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class);
              startActivity(startApp)
            }, 3000)
    
    Login or Signup to reply.
  2. The correct way to create a Splash screen is as follows

    style.xml

     <style name="SplashStyle" parent="Theme.MaterialComponents.DayNight.NoActionBar">
            <item name="android:windowBackground">@drawable/splash</item>
        </style>
    

    Splash.xml

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@color/black" /> // background color
    
        <item
            android:drawable="@drawable/ic_launcher" /> // logo
            android:gravity="center" />
    
    </layer-list>
    

    Manifest

    <activity
                android:name=".SplashActivity"
                android:exported="true"
                android:noHistory="true"
                android:theme="@style/SplashStyle">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
    
            </activity>
    

    SplashActivity.java

    public class SplashActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    //        setContentView(R.layout.activity_splash); // No need setContentView
            
            Intent intent = new Intent(SplashActivity.this,MainActivity.class);
            startActivity(intent);
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search