安卓:测试广告未显示

4
我正在尝试给当前的活动添加广告,但是该活动没有显示任何广告。我还在Android Manifest.xml中添加了Internet权限。
以下是我的.Gradle文件
 apply plugin: 'com.android.application'

    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.0"

        defaultConfig {
            applicationId "sms.and.call.alert.flashlight.lpa.com.myapplication"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.google.android.gms:play-services:9.6.0'

        compile 'com.google.firebase:firebase-ads:9.6.0'
    }

这是MainActivity

  public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544~3347511713");
        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest request = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)        // All emulators
                  // An example device ID
                .build();
        mAdView.loadAd(request);

    }
}

这是MainActivity.xml

<?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"

    xmlns:tools="http://schemas.android.com/tools"
    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="sms.and.call.alert.flashlight.lpa.com.myapplication.MainActivity">
    <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>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

你需要在应用程序的 build.gradle 文件末尾添加 apply plugin: 'com.google.gms.google-services',而不是使用 compile 'com.google.android.gms:play-services:9.6.0'。同时,你需要将 google-services.json 文件放置到正确的位置。 - MatPag
我把JSON文件放在应用程序文件夹中,看起来没问题?但是报错说找不到带有该ID的插件。 - Kumail Hussain
请检查您的广告单元 ID 是否仅适用于横幅广告。如果它适用于插页式广告,则不会将广告加载到 adView 中。 - Shrenik Shah
好的,你在项目 build.gradle 依赖中添加了 classpath 'com.google.gms:google-services:3.0.0' 吗?如果不知道如何操作,请查看此链接 https://firebase.google.com/docs/android/setup#add_the_sdk - MatPag
添加后我收到了错误。 - Kumail Hussain
显示剩余14条评论
2个回答

7

Admob-Firebase初始配置指南

更新于10/04/2016 - MM/dd/yyyy

步骤1:确保已安装最新版本的Google Play服务和Google Repository SDK versions

步骤2:在Firebase控制台创建项目后,将google-services.json文件放入应用程序文件夹中。
更多信息:https://firebase.google.com/docs/android/setup#add_firebase_to_your_app

步骤3:按以下方式设置项目级build.gradle文件。
更多信息:https://firebase.google.com/docs/android/setup#add_the_sdk

buildscript {
    // ...
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'
        // ...
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

步骤 4:将您的 app 级 build.gradle 设置如下:
更多信息: https://firebase.google.com/docs/admob/android/quick-start#integrate_firebase_and_the_mobile_ads_sdk

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "your.package.app"
        minSdkVersion 9
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')

    compile 'com.google.firebase:firebase-ads:9.0.0'
}

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

步骤五:如果这些权限尚未存在,请将它们添加到AndroidManifest中

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

此外,请确保您在AndroidManifest中的包名称与应用程序build.gradle中的applicationId匹配,否则将出现编译错误。
第六步:继续按照快速入门教程并学习如何创建广告单元以在视图上显示广告:https://firebase.google.com/docs/admob/android/quick-start 第七步:如果您仍然遇到编译时问题,请查看以下非常有用且常见的线程和示例:
清单和applicationId问题:https://dev59.com/6-k6XIcBkEYKwwoYDPoQ#37177173
多个flavor问题: https://dev59.com/d1sW5IYBdhLWcg3wbWxv#34990999
Google Admob示例:https://github.com/googleads/googleads-mobile-android-examples

感谢您的帮助,遵循这些步骤让我成功展示广告 :) - Kumail Hussain
终于了!!我很高兴能帮助你。希望这些信息对其他人也有用 :) - MatPag
1
我忘记了我必须实现适当的网络权限。 - Aloha

0

也许您忘记通过调用加载广告:

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

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