Reputation: 926
The app I am creating has lots of pictures that the user will be able to scroll through, In the app there is an array of objects and each object contains a picture and a statistic. My app was running perfectly is my array would only contain 58 objects, however now when I build the app quits and bring up the error "Sorry, your application has stopped unexpectedly" and tho LogCat shows
10-25 18:46:24.578: ERROR/AndroidRuntime(491): FATAL EXCEPTION: main
10-25 18:46:24.578: ERROR/AndroidRuntime(491): java.lang.RuntimeException: Unable to
instantiate activity ComponentInfo{com.example.notes/com.example.notes.NotesListActivity}:
java.lang.ArrayIndexOutOfBoundsException
and here is my current code
public class NotesListActivity extends Activity implements OnClickListener
{
public boolean nextClicked = false;
public boolean prevClicked = false;
public int counter = 1;
public int nextImg;
public FunnyPic[] picArray = new FunnyPic[55];
{
picArray[0] = new FunnyPic(R.raw.img1, 0);
picArray[1] = new FunnyPic(R.raw.img2, 0);
.....
.....
.....
picArray[180] = new FunnyPic(R.raw.img181, 0);
picArray[181] = new FunnyPic(R.raw.img182, 0);
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button changePicButton = (Button) this.findViewById(R.id.button1);
changePicButton.setOnClickListener(this);
}
public void onClick(View view)
{
Log.i("onClick", "BEGIN");
ImageView image = (ImageView) findViewById(R.id.imageView1);
if(counter == picArray.length-1)
counter=0;
image.setImageResource(picArray[counter].getImg());
counter++;
Log.i("onClick", "END");
}
public void prevPicHandler(View traget)
{
if(counter==0)
{
counter = picArray.length-1;
}
counter--;
Log.i("onClick", "BEGIN");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(picArray[counter].getImg());
Log.i("onClick", "END");
}
}
please let me know if you have any suggestions. Also if anyone could recommend a more efficient way to put the objects into the array so I wont have to manually put them in each time I add new pictures.
Thanks
Upvotes: 0
Views: 115
Reputation: 17206
Why not store the FunnyPic objects in an ArrayList or Vector?
public ArrayList<FunnyPic> getFunnyPicArray()
{
ArrayList<FunnyPic> picList = new ArrayList<FunnyPic>();
picList.add(new FunnyPic(R.raw.img1, 0));
//..
return picList;
}
Upvotes: 0
Reputation: 44240
Your max index is 54
, yet in the sample code, you're accessing indices greater than that. Hm..I wonder what the problem could be!?
Upvotes: 0
Reputation: 1500645
You're creating an array with 55 elements:
public FunnyPic[] picArray = new FunnyPic[55];
The maximum valid index here is 54.
You're then trying to access element 181:
picArray[181] = new FunnyPic(R.raw.img182, 0);
How did you expect that to work? Have you considered using a List<FunnyPic>
instead?
Note that you've got an array variable declaration statement, and then separately an initializer block. You don't have an array initializer. If that's what you thought you had, you should have used:
public FunnyPic[] picArray =
{
new FunnyPic(R.raw.img1, 0),
new FunnyPic(R.raw.img2, 0),
...
new FunnyPic(R.raw.img182, 0)
};
Upvotes: 2