admob ads doesn't show up

2019-07-07 01:36发布

问题:

I made a simple app and added inside it an ad but it doesn't show up in the device.

and there is no errors in my code:

xml page:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:ads="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/RelativeLayout1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity" 
 >

        <com.google.android.gms.ads.AdView
    xmlns:app="http://schemas.android.com/apk/libs/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="66dp"
    ads:adSize="BANNER"
    ads:adUnitId="ad_ip" >
    </com.google.android.gms.ads.AdView>
    </RelativeLayout>

manifest file:

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" ></uses-permission>    


 <meta-data
android:name="com.google.android.gms.version"
android:value="4.3.23" />

<activity android:name="com.google.android.gms.ads.AdActivity"                
 android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" /> 

回答1:

I think the problem is here

 <com.google.android.gms.ads.AdView
    xmlns:app="http://schemas.android.com/apk/libs/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="66dp"
    ads:adSize="BANNER"
    ads:adUnitId="ad_ip" >

1) The xmlns attribute should be like this and in my case I have placed in the root tag of xml file.

        xmlns:ads="http://schemas.android.com/apk/res-auto"

2) Check, you have given the actual adUnitId from admob account.

Hope this will help you, if any further problems, you can ask.



回答2:

At the beginning I had made the same mistake, I only used the xml file and didn´t load the view in my Activity. So just put this inisde the activity:

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


回答3:

You are importing the wrong xmlns attribute inside your adview (it refers to legacy admob):

<com.google.android.gms.ads.AdView
xmlns:app="http://schemas.android.com/apk/libs/com.google.ads"

you should only use the following which refers to Google Play Admob:

xmlns:ads="http://schemas.android.com/apk/res-auto"

==> remove the wrong xmlns attribute



回答4:

Try this

XML:

       <com.google.ads.AdView
        android:id="@+id/ad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="66dp"
        ads:adSize="BANNER"
        ads:adUnitId="your key ID"
        ads:loadAdOnCreate="true"/>

Add following code in your manifest:

 <activity
            android:name="com.google.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

Hope this will help you..



标签: android admob