几秒钟的启动画面后,应用程序将一个活动移至另一个活动时崩溃

时间:2020-02-07 19:52:43

标签: java android

package com.example.firstclasswithahmad;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import java.security.PublicKey;
import javax.crypto.spec.DHPublicKeySpec;

public class SplashScreen extends AppCompatActivity {

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


    Thread td= new Thread(){
        public void run(){

            try {
              Thread.sleep(3000);
            } catch (Exception e){
               e.printStackTrace();
            } finally {
               Intent i=new Intent(SplashScreen.this, MainActivity.class);
               startActivity(i);
            }
        }
    };
    td.start();
  }
}

1 个答案:

答案 0 :(得分:0)

尝试在onPostCreate()方法内创建意图。像这样:

@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    Thread td = new Thread() {
        public void run() {

            try {
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);
            }
        }
    };
    td.start();
}