From 7c8957907f3237ebf245e9871e625868d1d77e25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oktay=20Sayg=C4=B1n?= <66143697+Sygn53@users.noreply.github.com> Date: Sat, 9 Sep 2023 13:21:41 +0300 Subject: [PATCH] fix(android): crash when activity context not available --- ...iveGoogleMobileAdsBannerAdViewManager.java | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsBannerAdViewManager.java b/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsBannerAdViewManager.java index 0a98025f..dd317fd9 100644 --- a/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsBannerAdViewManager.java +++ b/android/src/main/java/io/invertase/googlemobileads/ReactNativeGoogleMobileAdsBannerAdViewManager.java @@ -167,12 +167,16 @@ private BaseAdView initAdView(ReactNativeAdView reactViewGroup) { } BaseAdView adView; if (ReactNativeGoogleMobileAdsCommon.isAdManagerUnit(reactViewGroup.getUnitId())) { - // in order to display the debug menu for GAM ads we need the activity context - // https://github.com/invertase/react-native-google-mobile-ads/issues/188 - adView = - new AdManagerAdView( - Objects.requireNonNull( - ((ReactContext) reactViewGroup.getContext()).getCurrentActivity())); + if (((ReactContext) reactViewGroup.getContext()).getCurrentActivity() != null) { + // in order to display the debug menu for GAM ads we need the activity context + // https://github.com/invertase/react-native-google-mobile-ads/issues/188 + adView = + new AdManagerAdView( + Objects.requireNonNull( + ((ReactContext) reactViewGroup.getContext()).getCurrentActivity())); + } else { + return null; + } } else { adView = new AdView(reactViewGroup.getContext()); } @@ -255,24 +259,25 @@ private void requestAd(ReactNativeAdView reactViewGroup) { } BaseAdView adView = initAdView(reactViewGroup); - adView.setAdUnitId(unitId); - - reactViewGroup.setIsFluid(false); - if (adView instanceof AdManagerAdView) { - if (sizes.contains(AdSize.FLUID)) { - reactViewGroup.setIsFluid(true); - ((AdManagerAdView) adView).setAdSizes(AdSize.FLUID); + if (adView != null) { + adView.setAdUnitId(unitId); + reactViewGroup.setIsFluid(false); + if (adView instanceof AdManagerAdView) { + if (sizes.contains(AdSize.FLUID)) { + reactViewGroup.setIsFluid(true); + ((AdManagerAdView) adView).setAdSizes(AdSize.FLUID); + } else { + ((AdManagerAdView) adView).setAdSizes(sizes.toArray(new AdSize[0])); + } + if (manualImpressionsEnabled) { + ((AdManagerAdView) adView).setManualImpressionsEnabled(true); + } } else { - ((AdManagerAdView) adView).setAdSizes(sizes.toArray(new AdSize[0])); + adView.setAdSize(sizes.get(0)); } - if (manualImpressionsEnabled) { - ((AdManagerAdView) adView).setManualImpressionsEnabled(true); - } - } else { - adView.setAdSize(sizes.get(0)); - } - adView.loadAd(request); + adView.loadAd(request); + } } private void sendEvent(ReactNativeAdView reactViewGroup, String type, WritableMap payload) {