我想为我的Android应用程序制作启动画面。我在android studio中的welcomescreen.java中编写了以下代码。但在运行应用程序后,应用程序已停止。 :(我现在该怎么办?
package com.mateors.welcomescreen;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class WelcomeScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_screen);
Thread myThread = new Thread(){
@Override
public void run() {
try {
sleep(5000);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}
}
答案 0 :(得分:3)
private static int SPLASH_TIME_OUT = 1500;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(WelcomeScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
尝试上面的代码段
答案 1 :(得分:0)
尝试使用以下代码:
package com.mateors.welcomescreen;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class WelcomeScreen extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_screen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(WelcomeScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}