Reputation: 547
Recently I notice this fatal crash happens to my android app.
Fatal Exception: java.lang.IndexOutOfBoundsException Index: 1, Size: 0
The issue happens when user click into item page, and fail to load index 1 from my banner array and setting the image to drawable.
I tested the app with a few different brand android devices, but they all works fine without crashing. Most Importantly I was able to load the image from array and set the image.
The log is as below:
But then, it is so weird that only about 5% of my users are having this problem. (approximately 100-200 active users daily). One of my friend got this crash once when he loads into the item page. But he only crashed once, and he is a regular users that been using the app for couple session daily.
Here are the codes that I have for my item page:
(DetailActivity.java:222 = Picasso.with(this).load(sidebannerAry.get(sidebanner1index).get("image")).into(sidebanner1Btn); )
public class DetailActivity extends Activity{
public static ArrayList<HashMap<String,String>> sidebannerAry = new ArrayList<HashMap<String,String>>();
int sidebanner1index = 1;
}
protected void onCreate(Bundle savedInstanceState) {
sidebanner1Btn = (ImageView) findViewById(R.id.sidebanner1btn);
sidebanner1Btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent b = new Intent(Intent.ACTION_VIEW, Uri.parse(sidebannerAry.get(sidebanner1index).get("url")));
startActivity(b);
}
});
Picasso.with(this).load(sidebannerAry.get(sidebanner1index).get("image")).into(sidebanner1Btn);
}
And this is the code I use to retrieve data from database during app start up screen:
JSONArray sidebannerAry = result.getJSONArray("sidebannerData");
for (int i = 0; i < sidebannerAry.length(); i++){
JSONObject temp = sidebannerAry.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
String image = DefensiveClass.optString(temp, "bannerImage");
String url = DefensiveClass.optString(temp, "bannerURL");
map.put("image", image);
map.put("url", url);
DetailActivity.sidebannerAry.add(map);
}
Can anyone help me take a look and see if I did anything wrong? Thank you so much.
Upvotes: 0
Views: 238
Reputation: 2396
The error happens in the following line :-
(DetailActivity.java:222 = Picasso.with(this).load(sidebannerAry.get(sidebanner1index).get("image")).into(sidebanner1Btn); )
In the above line, the only part where an IndexOutOfBoundsException
can occur is this :-
sidebannerAry.get(sidebanner1index)
You've set the value of sidebanner1index
as "1". So, you're getting an error in the cases when the sidebannerAry
has less than 2 elements. That is why only a certain percentage of your users is getting this error.
Index out of bounds error happens when you try to access an index that is higher than the length of the data structure.
The easiest way to solve it is to put a flag before the Picasso line that checks wether the sidebannerAry
has a length that is higher than the sidebannerIndex
.
Upvotes: 0