AdMob 横幅广告未显示

AdMob banner ad not showing

我在我的应用程序中使用 admob,它工作正常,但突然完全停止显示广告。 下面是我使用的代码:

mainActivity.xml:

<com.google.android.gms.ads.AdView
        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="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

MainActivity.java

        AdView mAdView = (AdView) findViewById(R.id.adView);

        AdRequest adRequest = new AdRequest.Builder().build();

        mAdView.loadAd(adRequest);

AndroidManifest:

 <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="19" />

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

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

<!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />

我遵循了 adMob 网站上的所有说明,正如我几天前所说的那样。

下面是来自 logcat 的几行,可能与我的问题有关:

09-25 22:44:50.292: E/dalvikvm(18554): Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza

09-25 22:45:07.209: E/Ads(18554): JS: Uncaught ReferenceError: renderAd is not defined (https://googleads.g.doubleclick.net/mads/gma:1)
09-25 22:45:07.209: E/Web Console(18554): Uncaught ReferenceError: renderAd is not defined:1
09-25 22:45:07.209: E/Ads(18554): JS: Uncaught ReferenceError: onLoad is not defined (https://googleads.g.doubleclick.net/mads/gma:1)
09-25 22:45:07.209: E/Web Console(18554): Uncaught ReferenceError: onLoad is not defined:1

我反复收到下面的警告,当我点击应该显示广告的空白区域时,它出现得更多

09-25 22:46:10.911: W/PicturePileLayerContent(18554): Warning: painting PicturePile without content!

这是用 "ads"

过滤的 logcat
09-25 23:39:25.948: I/Ads(28874): Starting ad request.
09-25 23:39:25.948: I/Ads(28874): Use AdRequest.Builder.addTestDevice("8FCD71CAAE776558876AAA9BA964245A") to get test ads on this device.

09-25 23:39:26.919: I/Ads(22050): App index is not enabled
09-25 23:39:30.432: E/Ads(28874): JS: Uncaught ReferenceError: renderAd is not defined (https://googleads.g.doubleclick.net/mads/gma:1)
09-25 23:39:30.442: E/Ads(28874): JS: Uncaught ReferenceError: onLoad is not defined (https://googleads.g.doubleclick.net/mads/gma:1)
09-25 23:39:30.442: I/Ads(28874): Scheduling ad refresh 60000 milliseconds from now.
09-25 23:39:30.472: I/Ads(28874): Ad finished loading.

我到处搜索解决方案,也查看了一些类似但没有一个对我有帮助。

检查您是否有最新版本的 adMob 和 GooglePlayServices 库。

使用下面的代码行

AdView mAdView = (AdView) findViewById(R.id.adView);

AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)        // All emulators
.addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4")  // My Galaxy Nexus test phone
.build();


mAdView.loadAd(request );

而不是使用这个

AdRequest adRequest = new AdRequest.Builder().build();

因为当您的应用程序准备好在 Google Play 商店发布时将使用此行。

您可以用您的设备 ID 代替 "AC98C820A50B4AD8A2106EDE96FB87D4"。获取设备ID的代码:

String android_id = Settings.Secure.getString(getContext().getContentResolver(),
            Settings.Secure.ANDROID_ID);

请仔细阅读:

https://developers.google.com/admob/android/targeting#test_ads

https://developers.google.com/admob/android/quick-start

看到这个

像这样将横幅放在 LinearLayout 中,我遇到了同样的问题,我通过将它放在线性布局中修复了它

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

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-8056472942589154/2652831823"
        android:layout_alignBottom="@+id/saida"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</LinearLayout>

此问题已通过手动卸载应用程序并在不做任何更改的情况下重新安装来解决!

在我的例子中,广告没有显示,直到我为它留出足够的空间。

之前是

<LinearLayout
        android:orientation="vertical"
        android:background="@color/primary"
        android:layout_width="match_parent"
        android:layout_height="90dip">
           <ImageView
                android:onClick="goBack"
                android:clickable="true"
                android:src="@drawable/ic_arrow_back_white_48dp"
                android:layout_width="50dip"
                android:layout_height="50dip"/>
           <com.google.android.gms.ads.AdView
                android:layout_gravity="center_horizontal"
                android:id="@+id/adView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                ads:adSize="BANNER"
                ads:adUnitId="@string/dwn_bnr_ad"/>

此处 adview 的高度为 wrap_content,因此它会自行调整以达到其高度,但其父项 (LinearLayout) 的高度严格为 90dip,这不允许 adView 占用足够的高度 space 展示广告。

所以我所做的只是将 LinearLayout 的高度更改为 wrap_content 然后一切都很好

您是否返回到您的 admob 帐户注册您的特定应用以获取广告并获得一个新的更长的发布商编号,该编号带有 'ca-app-pub-' 前言而不是 'pub-' 前言?

在 Google Admob "Google Mobile Ads SDK" 开发网站的 "banner ads 1" 说明中没有任何地方提到必须返回您的 admob 帐户才能执行此操作。

这个愚蠢的错误让我耽搁了好几天。

在我的例子中,我在项目级别 buildscript 部分下有下面的代码 build.gradle

确保 maven {url "https://maven.google.com"}allProjects > repositories 下:

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

Make sure that you have added Banner Ad Unit in your Admob Account and used banner appUnit-id in your adUnitId of your app where Banner Ads are displayed .