"System.NullReferenceException: Object reference not set to an instance of an object" when trying to insert ads using google AdMob Xamarin Android

2k views Asked by At

I've been having issues with using Ads on xamarin for android, was hoping someone with more expertise could offer some advice. This is the first App I have build using Xamarin Android, or at least attempted to. Within the layout I have left a space for an advert. I've followed multiple guides including;

How to integrate AdMob ads in the latest MonoGame Android (XNA)?

https://blog.tommyparnell.com/admob-with-xamarin-part-1-banner-ads/

Both guides I've followed to the letter and I'm getting the same error each time I try to run the application

The syntax in my main activity is as follows;

    AdRequest adRequest = new AdRequest.Builder().Build();
    layout.LoadAd(adRequest);

When this is included, I get a runtime error as follows;

    Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable:      System.NullReferenceException: Object reference not set to an instance of an object
    06-05 09:39:21.062 E/AndroidRuntime( 7212): at MyApp.Android.MainActivity.OnCreate (Android.OS.Bundle) [0x00028] in c:\Users\User\Dropbox\Projects\MyApp_Xamarin\MyApp.Android\MainActivity.cs:44

I have tried deleting and recreating the google play services reference. I've ensured my manifest has the relevant activity tags within it

    <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

I'm at a total loss. The error I'm receiving looks like a java error, although as I'm in the c# environment I can't directly access and debug this that I can tell. After 3 days of getting no further with this issue I've turned to StackOverflow, if anyone can point me in the right direction I'd be very grateful!

I am using visual studio 2013

Cheers.

1

There are 1 answers

0
valdetero On

I've added admob to a Xamarin Forms app but not an XNA game. Here is a link to my app on github

var adMobElement = Element as AdMobBuddyControl;
if ((adMobElement != null) && (e.OldElement == null))
{
    AdView ad = new AdView(this.Context);
    ad.AdSize = AdSize.Banner;
    ad.AdUnitId = adMobElement.AdUnitId;
    var requestbuilder = new AdRequest.Builder();
    ad.LoadAd(requestbuilder.Build());
    this.SetNativeControl(ad);
}

Make sure to have this in your application tag of manifest:

<meta-data android:name="com.google.android.gms.version" 
           android:value="@integer/google_play_services_version" />
<activity android:name="com.google.android.gms.ads.AdActivity" 
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

And this permission:

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

I kind of fumbled my way through it. You may not need everything that I mentioned but hopefully it can give you a starting point.