4

我有一个问题SplashScreenActivity,当我在手机上启动我的应用程序时,它会显示大约 0.5 秒的白屏。MainActitivy扩展FragmentActivity并在I中AndroidManifest声明SplashScreenActivity为启动器和纵向模式为screenOrientation.

编码:

public class SplashScreenActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.splashscreen);
    randomSplash();

    Thread splashscreen = new Thread() {

        public void run() {
            try {
                Thread.sleep(1000);
                Intent mainScreen = new Intent("com.rm.jkrm.MAINACTIVITY");
                startActivity(mainScreen);
            } catch (InterruptedException e) {

            } finally {
                finish();
            }
        }
    };
    splashscreen.start();
}

private void randomSplash(){
    Random random = new Random();
    int i = random.nextInt(4);

    LinearLayout ln = (LinearLayout) findViewById(R.id.splashscreen);

    switch(i){
    case 1: 
        ln.setBackgroundResource(R.drawable.splash1);
        break;
    case 2: 
        ln.setBackgroundResource(R.drawable.splash2);
        break;
    case 3: 
        ln.setBackgroundResource(R.drawable.splash3);
        break;
    default: 
        ln.setBackgroundResource(R.drawable.splash0);
        break;
    }
}
}

XML:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/splashscreen"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
</LinearLayout>
4

3 回答 3

1

您需要在 AsyncTask 中运行这两个操作:

setContentView(R.layout.splashscreen);
randomSplash();

setContentViewdoInBackground-method 和您运行的 postExecute 方法放入randomSplash

于 2013-02-25T13:27:32.967 回答
1
 Thread splashscreen = new Thread() {

        public void run() {
            try {
                Thread.sleep(1000);
                Intent mainScreen = new Intent("com.rm.jkrm.MAINACTIVITY");
                startActivity(mainScreen);
            } catch (InterruptedException e) {

            } finally {
                finish();
            }
        }
    };
    splashscreen.start();

这是你的问题 UI 线程睡眠不是一个很好的主意,而是使用处理程序,我认为它也可能导致异常。

Handler h=new Handler();
        h.postDelayed(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub
                startActivity(new Intent(Splash_Activity.this,Main_Activity.class));
                finish();
            }
        }, 2000);
    }
于 2013-02-25T13:08:39.457 回答
1

将 AndroidManifest.xml 文件中的 SplashActivity 主题更改为此。

android:theme="@android:style/Theme.Translucent.NoTitleBar"
于 2016-06-07T19:01:00.383 回答