当我的 Android 应用程序的初始屏幕打开时,应用程序崩溃

When the splash screen of my Android application opens the application crashes

最初,我的应用程序的第一个版本运行良好。 后来,在更改为侧边栏抽屉应用程序模板后,它无法正常运行,导致应用程序崩溃(由于闪屏)。 可能是什么问题?

这是 SpashScreen.java 文件的代码:

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

}

这是清单:

<?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>

试试这个

new Handler().postDelayed(new Runnable() {

        /*
         * Showing splash screen with a timer. This will be useful when you
         * want to show case your app logo / company
         */

        @Override
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity
            Intent i = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(i);

            // close this activity
            finish();
        }
    }, 3000);// time in milliseconds

试试这个可能会有帮助

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }, 3000);

尝试使用:

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

此代码不创建新线程, P.S。 Android 无法从另一个线程开始 activity(仅形式 UI)

startActivity()main thread 中需要 运行。使用 postDelay()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);

使用处理程序可以解决这个问题,请执行下面的代码,它将为您工作

  handler.postDelayed(
            new Runnable() {
                public void run() {
                    Intent intent = new Intent(youractivity.this, MainActivity.class);
            startActivity(intent);
                }
            }, 3000);