-1

处理程序 hnd = 新处理程序() {

    @Override
    public void handleMessage(Message msg) {

        int id = sequence.get(msg.arg1);

        if(msg.arg1 % 2 == 0) {
            sq.get(id-1).setBackgroundResource(R.drawable.square_show);
        } else {
            sq.get(id-1).setBackgroundResource(R.drawable.square);
        }
    }
};

@Override
public void onResume() {
    super.onResume();

    Thread background = new Thread(new Runnable() {
        public void run() {
            try {
                for(int i = 0; i <= sequence.size()-1; i++) {
                    Thread.sleep(200);
                    Message msg = hnd.obtainMessage();
                    msg.arg1 = i;
                    msg.setTarget(hnd); // EDITED
                    msg.sendToTarget();
                    record_tv.setText(""+i);
                }
            } catch(Throwable t) {

            }
        }
    });

    background.start();
}

代码到达 msg.sendToTarget(),做它的事情,然后再也没有回来

4

1 回答 1

2

sendToTarget(); 如果您没有设置接收器,则会引发空指针异常setTarget(Handler)

另外,在你的代码中我看到

record_tv.setText(""+i);

这一行,在你的线程内会抛出一个

android.view.ViewRoot$CalledFromWrongThreadException
于 2013-05-17T15:05:10.280 回答