public class MainActivity extends Activity {
TextView textview;
HandlerThread myThread = new HandlerThread("my");
Handler myHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView)findViewById(R.id.text);
myThread.start();
myHandler = new Handler(myThread.getLooper());
Toast.makeText(MainActivity.this,"in act = " + Thread.currentThread().getId(), Toast.LENGTH_SHORT).show();
myHandler.post(new Runnable() {
@Override
public void run() {
textview.setText("gzw " + Thread.currentThread().getId() + Thread.currentThread().getName());
}
});
new Thread(){
@Override
public void run() {
String s = textview.getText().toString();
textview.setText(s + "\n gzw2 " + Thread.currentThread().getId() + Thread.currentThread().getName());
}
}.start();
}
在两个run()方法中工作正常!为什么不从callwrongthreadexception调用? textview可以在两个线程中更改 第一次敬酒“in act = 1” textview是: gzw 516my gzw 517Thread-517