From ad38a05a1e004d332e928cc692a9307c8adf1192 Mon Sep 17 00:00:00 2001 From: Bayon Mbayo Date: Sat, 16 Nov 2024 19:31:18 +0100 Subject: [PATCH] Fix bugs: Null Exception in BannerExcutor --- .../admob/banner/BannerExecutor.java | 114 ++++++++++-------- 1 file changed, 65 insertions(+), 49 deletions(-) diff --git a/android/src/main/java/com/getcapacitor/community/admob/banner/BannerExecutor.java b/android/src/main/java/com/getcapacitor/community/admob/banner/BannerExecutor.java index c979869f..c09a9126 100644 --- a/android/src/main/java/com/getcapacitor/community/admob/banner/BannerExecutor.java +++ b/android/src/main/java/com/getcapacitor/community/admob/banner/BannerExecutor.java @@ -215,7 +215,12 @@ private void updateExistingAdView(AdOptions adOptions) { .runOnUiThread( () -> { final AdRequest adRequest = RequestHelper.createRequest(adOptions); - mAdView.loadAd(adRequest); + // TODO + if(mAdView != null){ + mAdView.loadAd(adRequest); + }else{ + Log.e(logTag, "mAdView is null, the updateExistingAdView can't be completed"); + } } ); } @@ -237,57 +242,68 @@ private void createNewAdView(AdOptions adOptions) { mAdViewLayout.addView(mAdView); // Start loading the ad. mAdView.loadAd(adRequest); - mAdView.setAdListener( - new AdListener() { - @Override - public void onAdLoaded() { - final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(mAdView); - - notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); - notifyListeners(BannerAdPluginEvents.Loaded.getWebEventName(), emptyObject); - super.onAdLoaded(); - } - - @Override - public void onAdFailedToLoad(@NonNull LoadAdError adError) { - if (mAdView != null) { - mViewGroup.removeView(mAdViewLayout); - mAdViewLayout.removeView(mAdView); - mAdView.destroy(); - mAdView = null; + // TODO + if(mAdView != null){ + mAdView.setAdListener( + new AdListener() { + @Override + public void onAdLoaded() { + final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(mAdView); + + notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); + notifyListeners(BannerAdPluginEvents.Loaded.getWebEventName(), emptyObject); + super.onAdLoaded(); + } + + @Override + public void onAdFailedToLoad(@NonNull LoadAdError adError) { + if (mAdView != null) { + mViewGroup.removeView(mAdViewLayout); + mAdViewLayout.removeView(mAdView); + mAdView.destroy(); + mAdView = null; + } + + final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(0, 0); + notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); + + final AdMobPluginError adMobPluginError = new AdMobPluginError(adError); + notifyListeners(BannerAdPluginEvents.FailedToLoad.getWebEventName(), adMobPluginError); + + super.onAdFailedToLoad(adError); + } + + @Override + public void onAdOpened() { + // TODO + notifyListeners(BannerAdPluginEvents.Opened.getWebEventName(), emptyObject); + super.onAdOpened(); + } + + @Override + public void onAdClosed() { + notifyListeners(BannerAdPluginEvents.Closed.getWebEventName(), emptyObject); + super.onAdClosed(); + } + + @Override + public void onAdImpression() { + notifyListeners(BannerAdPluginEvents.AdImpression.getWebEventName(), emptyObject); + super.onAdImpression(); + } } - - final BannerAdSizeInfo sizeInfo = new BannerAdSizeInfo(0, 0); - notifyListeners(BannerAdPluginEvents.SizeChanged.getWebEventName(), sizeInfo); - - final AdMobPluginError adMobPluginError = new AdMobPluginError(adError); - notifyListeners(BannerAdPluginEvents.FailedToLoad.getWebEventName(), adMobPluginError); - - super.onAdFailedToLoad(adError); - } - - @Override - public void onAdOpened() { - notifyListeners(BannerAdPluginEvents.Opened.getWebEventName(), emptyObject); - super.onAdOpened(); - } - - @Override - public void onAdClosed() { - notifyListeners(BannerAdPluginEvents.Closed.getWebEventName(), emptyObject); - super.onAdClosed(); - } - - @Override - public void onAdImpression() { - notifyListeners(BannerAdPluginEvents.AdImpression.getWebEventName(), emptyObject); - super.onAdImpression(); - } - } - ); + ); + } else{ + Log.e(logTag, "mAdView is null, the listener can't work"); + } + // Add AdViewLayout top of the WebView - mViewGroup.addView(mAdViewLayout); + if(mViewGroup != null){ + mViewGroup.addView(mAdViewLayout); + }else{ + Log.e(logTag, "mViewGroup is null, the initialization can't be completed"); + } } ); }