public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView text;
private Button changeText;
public static final int UPDATE_TEXT = 1;
public static final int UPDATE_TEXT2 =2;
private Handler handler = new Handler(){
public void handleMessage(Message msg){
switch (msg.what){
case UPDATE_TEXT:
text.setText("Nice to meet you");
// super.handleMessage(msg);
break;
case UPDATE_TEXT2:
text.setText("Nice to meet meme mememe emmememe");
// super.handleMessage(msg);
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
changeText = (Button) findViewById(R.id.change_text);
changeText.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.change_text:
new Thread(new Runnable() {
Message message = new Message();
public void run() {
for (int i = 0; i < 20; i++) {
//message.what = UPDATE_TEXT2;
handler.sendEmptyMessage(1);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// message.what = UPDATE_TEXT;
handler.sendEmptyMessage(2);
}
}
}).start();
break;
default:
break;
}
}
}
AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: The specified message queue synchronization barrier token has not been posted or has already been removed.
想在子线程中定时改变UI内容
运行出错,请问是什么问题?