diff --git a/java/advanced/BannerRecyclerViewExample/app/src/main/java/com/google/android/gms/example/bannerrecyclerviewexample/MainActivity.java b/java/advanced/BannerRecyclerViewExample/app/src/main/java/com/google/android/gms/example/bannerrecyclerviewexample/MainActivity.java index 76478061f..bdae88630 100644 --- a/java/advanced/BannerRecyclerViewExample/app/src/main/java/com/google/android/gms/example/bannerrecyclerviewexample/MainActivity.java +++ b/java/advanced/BannerRecyclerViewExample/app/src/main/java/com/google/android/gms/example/bannerrecyclerviewexample/MainActivity.java @@ -107,6 +107,10 @@ protected void onDestroy() { private void addBannerAds() { // Loop through the items array and place a new banner ad in every ith position in // the items List. + + // Using < instead of <= prevents the loop from accessing an index equal to the size of the list, avoiding IndexOutOfBoundsException by staying within the list's bounds. + // for (int i = 0; i < recyclerViewItems.size(); i += ITEMS_PER_AD) { + for (int i = 0; i <= recyclerViewItems.size(); i += ITEMS_PER_AD) { final AdView adView = new AdView(MainActivity.this); adView.setAdSize(AdSize.BANNER);