Handler.post小问题
package mars.handlertest;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
public class ActivityHandler extends Activity {
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler.post(r);
setContentView(R.layout.main);
System.out.println("activity----" + Thread.currentThread().getId());
System.out.println("activity----" + Thread.currentThread().getName());
}
Runnable r = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("handler----" + Thread.currentThread().getId());
System.out.println("handler----" + Thread.currentThread().getName());
try {
Thread.sleep(1000000);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
};
}
程序运行后,打印出了四条输出语句,而手机屏幕上显示的内容:“Hello World”却还没显示出来,打印语句的顺序是:
System.out.println("activity----" + Thread.currentThread().getId());
System.out.println("activity----" + Thread.currentThread().getName());
System.out.println("handler----" + Thread.currentThread().getId());
System.out.println("handler----" + Thread.currentThread().getName());
这样的话setContentView(R.layout.main);不就等于没执行吗? android handler post --------------------编程问答--------------------
补充:Java , J2ME