Reputation: 148
I'm trying to develop an android application where images can be set invisible one by one each 3 seconds.I tired doing it using following code.
final ImageView[] i = new ImageView[6];
public int l=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cs_game_two);
i[0] = (ImageView) findViewById(R.id.imageView2);
i[1] = (ImageView) findViewById(R.id.imageView3);
i[2] = (ImageView) findViewById(R.id.imageView4);
i[3] = (ImageView) findViewById(R.id.imageView5);
i[4] = (ImageView) findViewById(R.id.imageView6);
i[5] = (ImageView) findViewById(R.id.imageView7);
image_set();
}
public void image_set()
{
for( l=0; l<6; l++){
i[l].postDelayed(new Runnable() {
public void run() {
i[l].setVisibility(View.INVISIBLE);
}
}, 3000);
}
}
In this im getting ArrayIndexOutOfBoundsException
error.When i change for loop condition to l<5
only i[5] image will be set invisible.I can't seem to understand how to solve this please help.
Upvotes: 0
Views: 632
Reputation: 22173
You need to create a class that implements Runnable and pass the ImageView as constructor parameter. Currently the value can change and really a mess can happen.
Example:
private class MyRun implements Runnable {
private ImageView iv;
public MyRun(ImageView v){
iv = v;
}
@Override
public void run() {
iv.setVisibility(View.INVISIBLE);
}
}
Upvotes: 1