From 39da308fbd9f9dddd14f16065ac1aff27804f6ee Mon Sep 17 00:00:00 2001 From: theoyuncu8 <97362820+theoyuncu8@users.noreply.github.com> Date: Sun, 4 Feb 2024 01:59:08 +0300 Subject: [PATCH] Update MainActivity.java IndexOutOfBoundsException --- .../gms/example/bannerrecyclerviewexample/MainActivity.java | 4 ++++ 1 file changed, 4 insertions(+) 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);