Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 睡眠时启动屏幕为空白_Java_Android_Sleep_Wait_Splash Screen - Fatal编程技术网

Java 睡眠时启动屏幕为空白

Java 睡眠时启动屏幕为空白,java,android,sleep,wait,splash-screen,Java,Android,Sleep,Wait,Splash Screen,我试图让飞溅布局出现5秒钟,然后切换到主菜单布局。屏幕空白约5秒钟,然后弹出主菜单布局。如果我只是在不睡觉的情况下运行splash布局,它运行的很好,所以我认为这不是问题所在。有什么想法吗 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splas

我试图让飞溅布局出现5秒钟,然后切换到主菜单布局。屏幕空白约5秒钟,然后弹出主菜单布局。如果我只是在不睡觉的情况下运行splash布局,它运行的很好,所以我认为这不是问题所在。有什么想法吗

   @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mainMenu();
    }




    private void mainMenu()
    {
        setContentView(R.layout.mainmenu);

    }

这是因为您正在主UI线程上执行Thread.sleep。。那是不推荐的

改用定时器,使用下面的代码

private Timer timer;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    handler = new Handler();

        timer = new Timer();
        TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    YourActivity.this.setContentView(R.layout.mainmenu);
                }
            });

        }
    };
    timer.schedule(timerTask, 5000);
}
复制品?