AdMob not working in app

2019-09-01 16:10发布

问题:

So, my problem that AdMob banner not working in my app, I have done exactly like here: https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start

and when I start my app on my phone there is no banner at all, what I can do to fix this? My publisher id correct, I checked it several times.

Here my code of layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"

    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff055500"
    android:paddingLeft="50dp"
    android:paddingRight="50dp"
    android:paddingBottom="5dp"
    android:clickable="false"
  >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/name"
            android:id="@+id/textView"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="65dp"
            android:textColor="#ffffffff"
            android:textSize="27sp"/>


        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/btnGetAdviceMain"
            android:id="@+id/btnGetAdvice"
            style="?android:attr/borderlessButtonStyle"
            android:layout_below="@+id/textView"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="55dp"
            android:onClick="clickOnGetAdvice"
            android:background="@drawable/shape_selector"
            android:textColor="#ffffffff"/>


        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/btnAbout"
            style="?android:attr/borderlessButtonStyle"
            android:id="@+id/btnAbout"
            android:onClick="clickOnAbout"
            android:background="@drawable/shape_selector"
            android:textColor="#ffffffff"
            android:layout_below="@+id/btnGetAdvice"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="5dp" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/btnRate"
            style="?android:attr/borderlessButtonStyle"
            android:id="@+id/btnRate"
            android:background="@drawable/shape_selector"
            android:textColor="#ffffffff"
            android:layout_below="@+id/btnAbout"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="5dp"
            android:onClick="clickOnRateApp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/quote"
            android:id="@+id/quote"
            android:textColor="#ffffffff"
            android:textSize="16sp"
            android:layout_marginTop="42dp"
            android:layout_below="@+id/btnRate"
            android:layout_centerHorizontal="true" />

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ads:adUnitId="@string/banner_publisher_id"
            ads:adSize="SMART_BANNER"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">
            </com.google.android.gms.ads.AdView>

</RelativeLayout>

Here code in method OnCreate():

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

Also I have this in Manifest:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

回答1:

1 Check activity: Folowing code should place in onCreate method:

AdView adView = (AdView) this.findViewById(R.id.yourAdView);
AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("Your test device hash") // omit if none
        .build();
adView.loadAd(adRequest);

2 Check layout: Following code should place in your activity's layour xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="false"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/linearLayout">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="Your unit id"></com.google.android.gms.ads.AdView>
</LinearLayout>

3 Check AndroidManifest.xml: Following permissions should be declared above application tag:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


回答2:

Sir; it will work on some devices and not on some devices-(320dp wide devices) because there isn't enough space like you stated, when you include padding etc. so remove this code from your RelatvieLayout

android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingBottom="5dp" // you can ignore this one

so you will now have

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:ads="http://schemas.android.com/apk/res-auto"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#ff055500"
  android:paddingBottom="5dp" // you can delete this one
  android:clickable="false"
>

Official proof here