1

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.

Community
  • 1
  • 1
Aeptitude
  • 172
  • 3
  • 20
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – DGibbs Jun 05 '15 at 08:56
  • The line in question which is generating the error is line 44 at MainActivity is; layout.LoadAd(adRequest); which has been initialised above as; var layout = FindViewById(Resource.Id.mainAdView); which is in reference to the xml layout; – Aeptitude Jun 05 '15 at 09:00
  • Can you share your project? – Artur Shamsutdinov Jun 05 '15 at 11:14
  • I have the exact same problem trying to resolve it. Same exception at the same line, Xamarin up to date and all. The layout is not null so that can't be the problem. Mine was working before I upgraded my device to Android 5.0 and I noticed that my device ID has changed, don't know if it has anything to do with it. – PmanAce Jun 06 '15 at 05:40
  • Did you ever find a solution to this problem? – Chris Swain Jan 18 '16 at 19:37

1 Answers1

0

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.

valdetero
  • 4,624
  • 1
  • 31
  • 46