Only the original thread that created a view hierarchy can touch its views

来源:互联网 发布:interbase数据库 编辑:程序博客网 时间:2024/06/06 04:29

Android线程的问题

public class MainActivity extends Activity implements View.OnClickListener {      private static final int COMPLETED = 0;      private TextView stateText;      private Button btn;      private Handler handler = new Handler() {          @Override          public void handleMessage(Message msg) {              if (msg.what == COMPLETED) {                  stateText.setText("completed");              }          }      };      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          stateText = (TextView) findViewById(R.id.tv);          btn = (Button) findViewById(R.id.btn);          btn.setOnClickListener(this);      }      @Override      public void onClick(View v) {          new WorkThread().start();      }      //工作线程      private class WorkThread extends Thread {          @Override          public void run() {              //......处理比较耗时的操作              //处理完成后给handler发送消息              Message msg = new Message();              msg.what = COMPLETED;              handler.sendMessage(msg);          }      }  }  
阅读全文
0 0