【发布时间】:2013-02-07 21:04:10
【问题描述】:
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 没有获取启动 XML 布局文件的内容视图...现在,我怀疑这是一个线程问题,并且之前以某种方式执行了线程setContentView 方法虽然该方法位于代码中线程的运行方法之前,所以我这样想是不合逻辑的,但我觉得这个启动画面不起作用的原因已经用完了
【问题讨论】:
-
通过使用
run(),您仍然会阻塞 UI 线程。请改用.start()。还要记住,使用线程对于启动屏幕来说并不是那么好,您也可以尝试使用处理程序。但首先要评估是否需要喷溅。 -
非常感谢! :) 你能告诉我运行和启动的区别吗
-
调用
run()是直接方法调用。使用.start()实际上是在run()中的执行在单独的线程中运行。 -
再次非常感谢 A--C 非常感谢!!! :)
标签: java android multithreading thread-sleep