NZME / react-native-ad-manager

Google Ad Manager bridge for React Native
MIT License
52 stars 61 forks source link

[android] Error while updating property 'adUnitID' of a view managed by: CTKBannerView #75

Closed zibs closed 1 year ago

zibs commented 2 years ago

I've seen a number of background crashes with this stacktrace after upgrading to 2.0.1 from 1.3.5 (android only)

Exception com.facebook.react.bridge.JSApplicationIllegalArgumentException: Error while updating property 'adUnitID' of a view managed by: CTKBannerView
  at com.facebook.react.uimanager.ViewManagersPropertyCache$PropSetter.updateViewProp (ViewManagersPropertyCache.java:101)
  at com.facebook.react.uimanager.ViewManagerPropertyUpdater$FallbackViewManagerSetter.setProperty (ViewManagerPropertyUpdater.java:136)
  at com.facebook.react.uimanager.ViewManagerPropertyUpdater.updateProps (ViewManagerPropertyUpdater.java:56)
  at com.facebook.react.uimanager.ViewManager.updateProperties (ViewManager.java:47)
  at com.facebook.react.uimanager.ViewManager.createViewInstance (ViewManager.java:143)
  at com.facebook.react.uimanager.ViewManager.createView (ViewManager.java:76)
  at com.facebook.react.uimanager.NativeViewHierarchyManager.createView (NativeViewHierarchyManager.java:281)
  at com.facebook.react.uimanager.UIViewOperationQueue$CreateViewOperation.execute (UIViewOperationQueue.java:194)
  at com.facebook.react.uimanager.UIViewOperationQueue$1.run (UIViewOperationQueue.java:909)
  at com.facebook.react.uimanager.UIViewOperationQueue.flushPendingBatches (UIViewOperationQueue.java:1026)
  at com.facebook.react.uimanager.UIViewOperationQueue.access$2600 (UIViewOperationQueue.java:47)
  at com.facebook.react.uimanager.UIViewOperationQueue$2.runGuarded (UIViewOperationQueue.java:984)
  at com.facebook.react.bridge.GuardedRunnable.run (GuardedRunnable.java:31)
  at android.os.Handler.handleCallback (Handler.java:938)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loopOnce (Looper.java:226)
  at android.os.Looper.loop (Looper.java:313)
  at android.app.ActivityThread.main (ActivityThread.java:8751)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:571)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)
Caused by java.lang.reflect.InvocationTargetException:
  at java.lang.reflect.Method.invoke
  at com.facebook.react.uimanager.ViewManagersPropertyCache$PropSetter.updateViewProp (ViewManagersPropertyCache.java:93)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.admanager.AdManagerAdView.setAdUnitId(java.lang.String)' on a null object reference
  at com.matejdr.admanager.BannerAdView.setAdUnitID (BannerAdView.java:289)
  at com.matejdr.admanager.RNAdManagerBannerViewManager.setPropAdUnitID (RNAdManagerBannerViewManager.java:119)

I'm experimenting with a patch that looks like this:

diff --git a/node_modules/react-native-ad-manager/android/src/main/java/com/matejdr/admanager/BannerAdView.java b/node_modules/react-native-ad-manager/android/src/main/java/com/matejdr/admanager/BannerAdView.java
index a883edd..efb4c73 100644
--- a/node_modules/react-native-ad-manager/android/src/main/java/com/matejdr/admanager/BannerAdView.java
+++ b/node_modules/react-native-ad-manager/android/src/main/java/com/matejdr/admanager/BannerAdView.java
@@ -54,7 +54,6 @@ class BannerAdView extends ReactViewGroup implements AppEventListener, Lifecycle

     public BannerAdView(final Context context, ReactApplicationContext applicationContext) {
         super(context);
-        currentActivityContext = applicationContext.getCurrentActivity();
         applicationContext.addLifecycleEventListener(this);
         this.createAdView();
     }
@@ -71,9 +70,9 @@ class BannerAdView extends ReactViewGroup implements AppEventListener, Lifecycle

     private void createAdView() {
         if (this.adManagerAdView != null) this.adManagerAdView.destroy();
-        if (this.currentActivityContext == null) return;

-        this.adManagerAdView = new AdManagerAdView(currentActivityContext);
+        final Context context = getContext();
+        this.adManagerAdView = new AdManagerAdView(context);

         if (isFluid()) {
             AdManagerAdView.LayoutParams layoutParams = new AdManagerAdView.LayoutParams(
@@ -355,7 +354,6 @@ class BannerAdView extends ReactViewGroup implements AppEventListener, Lifecycle
     @Override
     public void onHostDestroy() {
         if (this.adManagerAdView != null) {
-            this.currentActivityContext = null;
             this.adManagerAdView.destroy();
         }
     }

Let me know if you've seen anything like this similarly, or what you're thinking is. It seems like 1.3.8+ would be effected by this.

stefandbd commented 2 years ago

Hey @zibs have you found any solution for this?

manojalwisnz commented 1 year ago

This issue if fixed in our latest version 2.0.4