Admob横幅广告未显示

5
我的Admob横幅广告没有显示出来,我已经按照这里的指南进行了操作: https://firebase.google.com/docs/admob/android/quick-start 主要活动(MainActivity)。
private AdView mAdView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544/6300978111");
    mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myapplication.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <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="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

在我的清单文件中,我拥有这个权限。
<uses-permission android:name="android.permission.INTERNET" />

Gradle 项目
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用程序Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.1'
    compile 'com.google.firebase:firebase-ads:10.0.1'
    compile 'com.google.firebase:firebase-core:10.0.1'
    testCompile 'junit:junit:4.12'
}



apply plugin: 'com.google.gms.google-services'

日志

01-31 20:44:49.586 3556-3556/? D/ucsBai_ConnectionManager: mPackageReceiver Action android.intent.action.PACKAGE_ADDED
01-31 20:44:58.836 19607-19607/com.example.myapplication I/FA: App measurement is starting up, version: 10084
01-31 20:44:58.836 19607-19607/com.example.myapplication I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
01-31 20:44:58.836 19607-19607/com.example.myapplication D/FA: Debug-level message logging enabled
01-31 20:44:58.836 19607-19607/com.example.myapplication D/FA: AppMeasurement singleton hash: 23949389
01-31 20:44:58.846 19607-19607/com.example.myapplication V/FA: Collection enabled
01-31 20:44:58.846 19607-19607/com.example.myapplication V/FA: App package, google app id: com.example.myapplication, 1:665936977222:android:13c3c6a65bcbec53
01-31 20:44:58.846 19607-19607/com.example.myapplication I/FA: To enable faster debug mode event logging run:
                                                                 adb shell setprop debug.firebase.analytics.app com.example.myapplication
01-31 20:44:58.906 19607-19607/com.example.myapplication V/FA: Registered activity lifecycle callback
01-31 20:44:58.916 19607-20434/com.example.myapplication V/FA: Using measurement service
01-31 20:44:58.916 19607-20434/com.example.myapplication V/FA: Connecting to remote service
01-31 20:44:59.816 19607-20434/com.example.myapplication V/FA: Connection attempt already in progress
01-31 20:45:00.116 19607-20434/com.example.myapplication D/FA: Connected to remote service
01-31 20:45:00.116 19607-20434/com.example.myapplication V/FA: Processing queued up service tasks: 2
01-31 20:45:05.156 19607-20434/com.example.myapplication V/FA: Inactivity, disconnecting from the service

有时候Admob需要一些时间才能显示横幅广告,我也遇到过这种情况,但是过了一段时间后就开始正常工作了。 - Awais Tariq
3个回答

3
首先打开https://apps.admob.com/,然后从中选择您的应用程序,现在从中选择“Monetize”选项卡,然后展开查看设置说明。

enter image description here

在这里,您需要输入您的应用程序ID。
MobileAds.initialize(getApplicationContext(), "ca-app-pub-1440645979698474~6985293144"); // replace with yours

对于XML,enter image description here

从广告单元中选择

<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="ca-app-pub-1440645979698474/3892225947"> 

请用这个 adUnitId 替换原有的内容即可完成。

2
您需要将广告单元ID替换为应用程序ID。
MobileAds.initialize(getApplicationContext(), here your app id will come );

这是您的广告标识符,与应用标识符不同。只需更改上面的代码并放置应用程序标识符即可。

这是您的广告单元标识符。

ca-app-pub-3940256099942544/6300978111

希望这能起作用。 干杯!


这是我的应用程序ID吗"com.example.myapplication"? - Alex
不是那个,它应该以 ca-app-pub 开头......只需进入 AdMob 帐户的设置和应用程序管理,点击后您将看到应用程序列表,在其中可以找到应用程序 ID。你明白我的意思吗? - Milind Vyas

1

请确保您已设置好付款/账单信息以便投放广告。请先使用提供的测试ID测试您的广告横幅。


我已经寻找这个原因2-3天了,发现这个问题很奇怪,它不是一个编程问题,实际上是admob的设置。如果您没有提交付款账户详细信息,则广告将无法启用,因此,如果您有新帐户,则必须填写付款账户详细信息以启用广告,这将需要大约24小时才能激活。请查看下面的链接。https://support.google.com/admob/answer/7652476?hl=en - Ghanshyam Bagul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接