AdMob Ads Not Showing

删除回忆录丶 提交于 2019-12-22 09:39:32

问题


So, I implemented AdMob ads with the Play Services SDK. I've done everything 'by the book', but the ads won't show.

If I set the AdView background to white, it shows the white space but not the Ad. I'm using Fragments, but I'm putting the AdView in the activity_main.xml (although in the fragments I've set a margin of 60dp at the bottom)

Here's my onCreate code (with the actual code and the 'testing' code)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);

    drawer.openDrawer(Gravity.LEFT);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));

    AdView adView = (AdView)this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("Device_ID")
            .build();
    adView.loadAd(adRequest);
}

And here's my activity_main.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.MainActivity">


<RelativeLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/adView" >

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ads:adUnitId="Unit_ID"
        ads:adSize="BANNER"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

<fragment
    android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

What am I missing?


回答1:


I think in addition to this block that you already have:

AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("Device_ID")
        .build();

...You are missing something like this:

    AdView adView = (AdView) this.findViewById(R.id.adView);
    adView.loadAd(adRequest);

Here is complete picture:

  1. what is in xml file:

    <com.google.android.gms.ads.AdView android:id="@+id/adView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      ads:adUnitId="your ad unit id"
      ads:adSize="BANNER"/>
    
  2. what is in onCreate:

    AdRequest adRequest = new AdRequest.Builder()
            .build();
    
    AdView adView = (AdView) this.findViewById(R.id.adView);
    adView.loadAd(adRequest);
    

Please note the code above is for production. You must use test device id for testing in order to avoid AdMob penalties.



来源:https://stackoverflow.com/questions/23848423/admob-ads-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!