Error displaying ads - Android C # Xamarin
I am trying to display AdMob ads in my application, but I am getting an error. I added Google Play Services and all my code is tested, I also use my own AdMob ID for testing, and all the code from the AdMod tutorial. Is there anything I might have missed?
Main Activity Announcement 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();
Adsense ad code for Main.axml:
<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:
+3
source to share
1 answer
Change the order. Your object mAdView
is Null because you didn't specify a contentView for 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
}
+2
source to share