Admob广告没有展示,甚至没有测试广告

时间:2018-11-10 10:26:27

标签: android admob

我没弄错我在做什么,我想在底部显示横幅,但甚至没有显示测试广告。我关注了their tutorial,我的应用如下所示:

  

带有广告的xml(确实会显示在预览中)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/infoPageBackgroundColor"
android:orientation="vertical"
android:paddingTop="8dp"
tools:context=".AppInfoActivity">

...

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    app:adSize="SMART_BANNER"
    app:adUnitId="@string/banner_id"/>

  

AndroidManifest中的元数据

<meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="@string/app_id"/>
  

gradle.build项目

buildscript {

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

}
dependencies {
    ...
}
}
  

应用gradle.build

dependencies {
...
}
  

参赛活动

import com.google.android.gms.ads.MobileAds;


public class EntryActivity extends BaseActivity {
private View progressBar;
private LoadListAsyncTask loadListAsyncTask;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    ...

    MobileAds.initialize(this, "@string/app_id");
}
  

Java文件

...

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

...


public class StickerPackListActivity extends BaseActivity {
...

private AdView mAdView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

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

我正在使用横​​幅广告测试ID进行尝试,它仅显示在xml编辑器中。知道为什么吗?即使我尝试输入自己的ID,它也无法正常工作。抱歉,无法像这样说明更多。我忘了什么吗?

2 个答案:

答案 0 :(得分:1)

您忘记初始化AdMob:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    MobileAds.initialize(this, "BANNER_ID_HERE");
    mAdView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    ...
}

https://developers.google.com/admob/android/banner

在物理设备上测试时,最近创建的AdMob标语有时无法加载,错误代码为2。您应该始终在模拟器上对其进行测试,以确保所有操作均已正确完成,以确保您的代码没有任何问题。

答案 1 :(得分:0)

您需要初始化Ads SDK:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ...
    // Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
    MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
}

https://firebase.google.com/docs/admob/android/quick-start

还要确保您的应用ID和横幅ID是正确的:

https://support.google.com/admob/answer/7356431?hl=en