当触发间隔为200毫秒或大于200毫秒时,是不会报错的,但是如果小一点,比如100,就有可能报错
郁闷啊是不是线程的问题,求解。public class Crazy02_layoutActivity extends Activity {
    /** Called when the activity is first created. */
private int[]colors={R.color.color7,
R.color.color6,
R.color.color5,
R.color.color4,
R.color.color3,
R.color.color2,
R.color.color1
};
private int[]names={R.id.view1,
R.id.view2,
R.id.view3,
R.id.view4,
R.id.view5,
R.id.view6,
R.id.view7};
private TextView[]views=new TextView[7];
private int currentColor=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        for(int i=0;i<names.length;i++)
        {
         views[i]=(TextView)findViewById(names[i]);
        }
        final Handler handler=new Handler(){ @Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what==0x2211)
{
for(int i=0;i<colors.length-currentColor;i++)
{
System.out.println("i="+i+",currentColor+i="+(currentColor+i));
views[i].setBackgroundResource(colors[currentColor+i]);

}
System.out.println("first end......");
for(int i=0,j=colors.length-currentColor;i<currentColor;i++,j++)
{
System.out.println("j="+j+",i="+i);
views[j].setBackgroundResource(colors[i]);
}
System.out.println("second end........");
}
super.handleMessage(msg);
}
        
        };
        new Timer().schedule(new TimerTask() {

@Override
public void run() {
// TOsdfAuto-generated method stub
currentColor++;
if(currentColor==7)
currentColor=0;
Message msg=new Message();
msg.what=0x2211;
handler.sendMessage(msg);
}
},0,140);
    }
}

解决方案 »

  1.   

                   
    currentColor++;
    if(currentColor==7)
       currentColor=0;这样的话currentColor有可能在一瞬间值是7.
    for(int i=0;i<colors.length-currentColor;i++)                        ....①
    {
        System.out.println("i="+i+",currentColor+i="+(currentColor+i));  ....②
        views[i].setBackgroundResource(colors[currentColor+i]);          ....③
        
    }在多线程下,有可能①的时候currentColor是6,到③就变成7了,那样就数组越界了.我觉得你不需要并行处理来完成你的功能.
    每次画面设置完以后再currentColor++好了.
      

  2.   

    在多线程下,有可能①的时候currentColor是6,到③就变成7了,那样就数组越界了.
    应该是异步操作的吧,
      

  3.   

    楼上看来把能说的都说了。
    if( condition ){ operations }condition和operations 中间可能被两个不同的线程插进去,结果么你懂的。
      

  4.   

    主线程和Timer线程共享currentColor变量,不加锁控制,不出问题才怪.