locked
Getting NullReferenceException at AdSizeCons.SmartBannerPortrait in Xamarin.Forms ios project RRS feed

  • Question

  • User396424 posted

    Hi,

    I am trying to add Banner Ads in my Xamarin.Form ios project, they work on simulator but if I connect my IPhone(debug mode) I get an this error -

    {System.TypeInitializationException: The type initializer for 'Google.MobileAds.AdSizeCons' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object. at Google.MobileAds.AdSizeCons.getBanner () <0x114527170 + 0x00030> in <62f0bd410ddd4c31bdeb77ff27efb070>:0 at Google.MobileAds.AdSizeCons..cctor () <0x114526c60 + 0x00000> in <62f0bd410ddd4c31bdeb77ff27efb070>:0 --- End of inner exception stack trace --- at Excercise.iOS.Renderers.AdMobViewRenderer.OnElementChanged (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs1[TElement] e) [0x00026] in C:\Users\xxxxx\Documents\Visual Studio Projects\Excercise - Copy\Excercise.iOS\Renderers\AdMobViewRenderer.cs:36 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].SetElement (TElement element) [0x00172] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:296 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:158 at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x00014] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Platform.cs:227 at Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded (Xamarin.Forms.VisualElement view) [0x0003e] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:100 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Load () [0x0001e] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:49 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].SetElement (TElement element) [0x0012e] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:283 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:158 at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x00014] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Platform.cs:227 at Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded (Xamarin.Forms.VisualElement view) [0x0003e] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:100 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Load () [0x0001e] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:49 at Xamarin.Forms.Platform.iOS.ScrollViewRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00087] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\ScrollViewRenderer.cs:71 at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x00014] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Platform.cs:227 at Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded (Xamarin.Forms.VisualElement view) [0x0003e] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:100 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Load () [0x0001e] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:49 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].SetElement (TElement element) [0x0012e] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:283 at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:158 at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x00014] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Platform.cs:227 at Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded (Xamarin.Forms.VisualElement view) [0x0003e] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:100 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Load () [0x0001e] in D:\a\1\s\Xamarin.Forms.Platform.iOS\VisualElementPackager.cs:49 at Xamarin.Forms.Platform.iOS.PageRenderer.ViewDidLoad () [0x0008f] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\PageRenderer.cs:237 at (wrapper managed-to-native) ObjCRuntime.Messaging.IntPtrobjcmsgSendSuper(intptr,intptr) at UIKit.UIViewController.getView () [0x0002a] in /Library/Frameworks/Xamarin.iOS.framework/Versions/14.0.0.0/src/Xamarin.iOS/UIKit/UIViewController.g.cs:3267 at Xamarin.Forms.Platform.iOS.PageRenderer.getNativeView () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\PageRenderer.cs:103 at Xamarin.Forms.Platform.iOS.PageRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00053] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\PageRenderer.cs:121 at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x00014] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Platform.cs:227 at Xamarin.Forms.Platform.iOS.NavigationRenderer.CreateViewControllerForPage (Xamarin.Forms.Page page) [0x00008] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:365 at Xamarin.Forms.Platform.iOS.NavigationRenderer.OnPushAsync (Xamarin.Forms.Page page, System.Boolean animated) [0x0001d] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:342 at Xamarin.Forms.Platform.iOS.NavigationRenderer.b470 (Xamarin.Forms.Page p) [0x00024] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:233 at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b70 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021 at (wrapper delegate-invoke) .invokevoid_object(object) at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/14.0.0.0/src/Xamarin.iOS/Foundation/NSAction.cs:178 --- End of stack trace from previous location where exception was thrown ---

    at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/14.0.0.0/src/Xamarin.iOS/UIKit/UIApplication.cs:86 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/14.0.0.0/src/Xamarin.iOS/UIKit/UIApplication.cs:65 at Excercise.iOS.Application.Main (System.String[] args) [0x00002] in C:\Users\xxxxxx\Documents\Visual Studio Projects\Excercise - Copy\Excercise.iOS\Main.cs:19 }

    Here is my code -

    Xamarin.forms -

    public class AdMobBannerAd : View {

    }
    

    ios project -

    AppDelegate.cs -

    public override bool FinishedLaunching(UIApplication app, NSDictionary options) { try { global::Xamarin.Forms.Forms.Init();

                App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height;
                App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width;
    
                MobileAds.Configure("ca-app-pub-xxxxxxxxxx~xxxxxxxxx");
    
                LoadApplication(new App());
            }
            catch(Exception ex)
            {
                string str = ex.Message;
            }
            return base.FinishedLaunching(app, options);
        }
    

    AdMobViewRendere.cs

    public class AdMobViewRenderer : ViewRenderer<AdMobBannerAd, BannerView>
    {
    
        string bannerId = "ca-app-pub-3940256099942544/2934735716";   // this is a test ad id
        BannerView adView;
        bool viewOnScreen;
    
        protected override void OnElementChanged(ElementChangedEventArgs<AdMobBannerAd> e)
        {
            base.OnElementChanged(e);
    
            if (e.NewElement == null)
                return;
    
            if (e.OldElement == null)
            {
                adView = new BannerView(AdSizeCons.SmartBannerPortrait)
                {
                    AdUnitId = bannerId,
                    RootViewController = GetRootViewController()
                };
    
                adView.AdReceived += (sender, args) =>
                {
                    if (!viewOnScreen) this.AddSubview(adView);
                    viewOnScreen = true;
                };
    
                var request = Request.GetDefaultRequest();
               // request.testDevices =[];
                adView.LoadRequest(request);
                base.SetNativeControl(adView);
            }
    
    
        }
    
        private UIViewController GetRootViewController()
        {
            foreach (UIWindow window in UIApplication.SharedApplication.Windows)
            {
                if (window.RootViewController != null)
                {
                    return window.RootViewController;
                }
            }
    
            return null;
        }
    }
    

    Is anyone having the same issue? Am I missing something or doing something wrong here?

    Thank you!

    Monday, October 26, 2020 4:06 PM

All replies

  • User369978 posted

    I don't know if it does matter but it seems Since ios14 it requires to app to use Google Mobile Ads SDK 7.64 or above which enables AppTrackingTransparency support , but the current latest version is still 7.57.0 , check the issue : https://github.com/xamarin/GoogleApisForiOSComponents/issues/433 .

    Tuesday, October 27, 2020 9:48 AM
  • User396424 posted

    The IPhone that I am using to test this is ios13 but its still not working. Also, for the initialization of the Ads SDK in the AppDelegate.cs it gives me green wiggly lines for MobileAds.Configure(string) is obsolete. So I changed it to the following and added my App ID in the info.plist.

    public override bool FinishedLaunching(UIApplication app, NSDictionary options) { try { MobileAds.SharedInstance.Start(CompletionHandler);

                global::Xamarin.Forms.Forms.Init();
    
                App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height;
                App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width;
    
                LoadApplication(new App());
            }
            catch(Exception ex)
            {
                string str = ex.Message;
            }
            return base.FinishedLaunching(app, options);
        }
    
        private void CompletionHandler(InitializationStatus status) { }
    

    Info.plist -

    GADApplicationIdentifier ca-app-pub-xxxxxxxxxxxx~xxxxxxxx GADIsAdManagerApp NSAppTransportSecurity NSAllowsArbitraryLoads NSAllowsArbitraryLoadsForMedia NSAllowsArbitraryLoadsInWebContent

    Can anyone tell me if this is the correct?

    Tuesday, October 27, 2020 1:21 PM