package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity {
private Intent myintent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
myintent = new Intent(this, MainActivity.class);
splashScreen(1000); }
public void splashScreen (final int x)
{
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(x);
} catch (InterruptedException e) {
e.printStackTrace();
}
startActivity(myintent);
finish();
}
}).run();
}
}
有代码,这就是问题:SplashScreen没有获取splash XML布局文件的内容视图......现在,我怀疑它是一个线程问题而且不知何故线程执行之前setContentView方法尽管该方法位于代码中Thread的run方法之前,所以我正在考虑这种方式是不合逻辑的但是我觉得这个Splash Screen没有工作原因
答案 0 :(得分:5)
将thread.run()
更改为thread.start()
:http://www.javafaq.nu/java-article1131.html
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(x);
} catch (InterruptedException e) {
e.printStackTrace();
}
startActivity(myintent);
finish();
}
}).start();
实施Splash的更好方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
myintent = new Intent(this, MainActivity.class);
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
startActivity(myintent);
finish();
}
}, 1000);
}
答案 1 :(得分:3)
package com.echo.myatlsnookpaid;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
public class Splash extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(3500);
// sleep(100);
}
catch(InterruptedException e){
e.printStackTrace();
} finally {
Intent openMain = new Intent(Splash.this, MainActivity.class);
startActivity(openMain);
finish();
}
}
};
timer.start();
}
@Override
protected void onPause() {
super.onPause();
finish();
}
}
并在你的清单中,给出
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
过滤他的splashactivity。