2017-05-23 27 views
3

我正在尝试在我的应用中展示AdMob广告,但我收到错误消息。 我已添加Google Play服务,并且所有代码都已检出,我还使用AdMob自己的测试广告ID,所有代码均来自AdMod指南。 有什么我可以错过?尝试展示广告时出现错误 - Android C#Xamarin

主要活动广告代码:

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代码:

<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> 

错误消息:

enter image description here

回答

2

变化的顺序。您的mAdView对象为空,因为您尚未指示活动的contentView。

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 
} 
+0

改变对不对的顺序? – DEFALT

+0

设置活动contentView,然后初始化您的Admod视图。查看我添加的“OnCreate”方法的代码。与你的比较。 – apineda

+0

啊,谢谢!我已upvoted和标记:) – DEFALT