handler老是出错,哪位帮忙看下!
public class MainActivity extends Activity {
private TextView num1;
private TextView num2;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (TextView)findViewById(R.id.num1);
num2 = (TextView)findViewById(R.id.num2);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
handler.post(runnable1);
handler.post(runnable2);
}
});
}
Handler handler = new Handler(){
public void handleMessage(Message msg){
num1.setText(msg.arg1);
num2.setText(msg.arg2);
}
};
Runnable runnable1 = new Runnable(){
int i = 0 ;
@Override
public void run() {
i+=1;
Message msg = handler.obtainMessage();
msg.arg1 = i;
//将Message对象加入到消息队列当中
handler.sendMessage(msg);
handler.postDelayed(runnable1, 500);
}
};
Runnable runnable2 = new Runnable(){
int i = 0 ;
@Override
public void run() {
i+=5;
Message msg = handler.obtainMessage();
msg.arg2 = i;
//将Message对象加入到消息队列当中
handler.sendMessage(msg);
handler.postDelayed(runnable2, 2000);
}
};
}