当我运行下面的代码时,应用程序不会下降:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
final Button b = (Button)findViewById(R.id.button1);
new Mt(b).start();
}
并且此代码下降(当我单击 button1 时出现错误“CalledFromWrongThreadException”):
@Override
protected void onCreate(Bundle savedInstanceState) {
....
final Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
new Mt(b).start();
}
});
}
Mt类在哪里
class Mt extends Thread{
Button b;
Mt(Button b){
this.b=b;
}
@Override
public void run() {
b.setText("4");
}
}
为什么第一个示例不会出现错误“CalledFromWrongThreadException”?