DEFALT
DEFALT

Reputation: 149

Error when trying to display ads - Android C# Xamarin

I am trying to display AdMob ads in my App, but I am getting an error. I have added Google Play Services, and all my code checks out, I am also using AdMob's own test Ad Id and all the code is from the AdMod guide. Is there anything I could have missed?

Main Activity Ad Code:

 protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            //AdMob
            mAdView = (AdView)FindViewById<AdView>(Resource.Id.adView);
            AdRequest adRequest = new AdRequest.Builder().Build();
            mAdView.LoadAd(adRequest);
            //AdMob

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            ActionBar.Hide();

Main.axml AdView Code:

 <com.google.android.gms.ads.AdView
          xmlns:ads="http://schemas.android.com/apk/res-auto"
          android:id="@+id/adView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_alignParentBottom="true"
          ads:adSize="BANNER"
          ads:adUnitId="HERE_MY_UNIT_ID">
  </com.google.android.gms.ads.AdView>

Error Message:

enter image description here

Upvotes: 3

Views: 109

Answers (1)

pinedax
pinedax

Reputation: 9346

Change the order. Your mAdView object is Null because you hadn't indicated the contentView of the Activity.

protected override void OnCreate(Bundle bundle)
{
        base.OnCreate(bundle);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        ActionBar.Hide();

        //AdMob
        mAdView = (AdView)FindViewById<AdView>(Resource.Id.adView);
        AdRequest adRequest = new AdRequest.Builder().Build();
        mAdView.LoadAd(adRequest);
        //AdMob
}

Upvotes: 2

Related Questions