1. 程式人生 > >線程練習中出現的錯誤

線程練習中出現的錯誤

block pda fine spa bsp 字符 nds nts eat


public
class MainActivity extends Activity { //1.define member variable Button bt; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //2.init init();
//3.listener bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new Thread() { public void run() { try { Thread.sleep(
1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } for(int i=0;i<10;i++) { tv.setText("After Update:
"+i); } } }; } }); } private void init() { // TODO Auto-generated method stub bt=(Button)this.findViewById(R.id.bt); tv=(TextView)this.findViewById(R.id.tv); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }

1.自己寫一遍線程的時候頭腦非常簡單,直接在子線程裏tv.setText("After Update:"+i);

你怕是個豬哦^(* ̄(oo) ̄)^

主線程和子線程不進行交互你怎麽會看得到哦

傻逼

2.Thread你不start怎麽讓人家運行呀 記得start

new Thread{
        //......
}.start();

3.

技術分享

運行之後字符串為啥這樣

技術分享

記得是msg.obj !!

4.是handleMessage不是HandleMessage

線程練習中出現的錯誤