Android:AdMob 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23089795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:45:34  来源:igfitidea点击:

Android: AdMob not working

androidadmobgoogle-play-services

提问by Markus

AdMob won't show in my app. Every time I invoke loadAd(..)I get the following errors:

AdMob 不会显示在我的应用中。每次调用时loadAd(..)都会出现以下错误:

Requesting resource 0x7f0b000e failed because it is complex. The Google Play services resources were not found. Check your project configuration to ensure that the resources are included. There was a problem getting an ad response. ErrorCode: 1

请求资源 0x7f0b000e 失败,因为它很复杂。未找到 Google Play 服务资源。检查您的项目配置以确保包含资源。获取广告响应时出现问题。错误代码:1

I searched hours for a solution to this problem but haven't found anything that works for me! I downloaded the latest version of the Google Play Servicesand added them as library in my project preferences

我搜索了几个小时来解决这个问题,但没有找到任何对我有用的东西!我下载了最新版本的Google Play Services并将它们添加为我的项目首选项中的库

Here is my code: Activity:

这是我的代码:活动:

public class WelcomeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_welcome);

        // Look up the AdView as a resource and load a request.
        AdView adView = (AdView) this.findViewById(R.id.welcomeAdView);

        AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
        adRequestBuilder.addTestDevice("************"); 
        adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
        AdRequest adRequest = adRequestBuilder.build();
        adView.loadAd(adRequest);
    }

}

}

Layout xml:

布局xml:

<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"
    xmlns:app="http://schemas.android.com/apk/res/xx.xx"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="xx.xx.WelcomeActivity" >

.... other layout stuff .....

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true" >

    <com.google.android.gms.ads.AdView
        android:id="@+id/welcomeAdView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="pub-***************" />
</RelativeLayout>

</RelativeLayout>

AndroidManifest.xml:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xx.xx"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.Light" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name="xx.xx.WelcomeActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        ... some more activities ...
        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >
        </activity>
    </application>

</manifest>

采纳答案by Libin

I don't find any issue with your code/layout. Issue could be with the Ads Unit Id.

我没有发现您的代码/布局有任何问题。问题可能与Ads Unit Id.

I would suggest you to try creating a different Ads Unit Id

我建议你尝试创建一个不同的 Ads Unit Id

Hope it works.

希望它有效。

回答by Reuben L.

Just adding this in case someone else has the same problem as mine.

只是添加这个以防其他人有和我一样的问题。

I tried using a new Ads Unit IDand that didn't work as well. In the end, the issue was because I was trying to fix an app that got suspended from Google Play and Admob had also actually stopped serving ads to applications with the specific package name / bundle ID.

我尝试使用新的Ads Unit ID,但效果不佳。最后,问题是因为我试图修复从 Google Play 暂停的应用程序,而 Admob 实际上也停止向具有特定package name / bundle ID.

Changing the package nameof the application solved the issue for me.

更改package name应用程序为我解决了这个问题。

回答by GeralexGR

Be careful of the ad-unit id string. My error was a space in the end of the adUnitId

请注意广告单元 ID 字符串。我的错误是 adUnitId 末尾有一个空格

回答by u1078792

In your layout file (activity_welcome.xml), the adview container (RelativeLayout) layout width/height looks strange to me. And I think it is useless in your use case.
Change from:

在您的布局文件 (activity_welcome.xml) 中,adview 容器 (RelativeLayout) 布局宽度/高度对我来说看起来很奇怪。而且我认为它在您的用例中毫无用处。
更改自:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true" >

    <com.google.android.gms.ads.AdView
        android:id="@+id/welcomeAdView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="pub-***************" />
</RelativeLayout>

to:

到:

<com.google.android.gms.ads.AdView
    android:id="@+id/welcomeAdView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="pub-***************" />

回答by Ievgenii Tkachenko

You used wrong ads:adUnitId="pub-***************". It must looks like ca-app-pub-3940256099942544/6300978111format. You can take it after creating banner by url https://apps.admob.com/#monetize/adunit:create

你用错了ads:adUnitId="pub-***************"。它必须看起来像ca-app-pub-3940256099942544/6300978111格式。您可以在通过 url https://apps.admob.com/#monetize/adunit:create创建横幅后使用它

回答by edgarmiro

Add this inside applicationtag in your Manifest file:

在您的清单文件中添加这个内部应用程序标签:

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

回答by Said Erraoudy

Also Your package might be blocked by Google AdMob. If you received a email like this:

此外,您的包裹可能被 Google AdMob 屏蔽了。如果您收到这样的电子邮件:

Hello,

你好,

This email is to alert you that one of your applications is not currently in compliance with our AdMob program policies and as a result, ad serving has been disabled to your application.

这封电子邮件是为了提醒您,您的一个应用程序目前不符合我们的 AdMob 计划政策,因此已停止向您的应用程序投放广告。

Ad serving has been disabled to: APPNAME (com.example.pack)

Action required: Check all other remaining applications in your account for compliance.

Current account status: Active .....

广告服务已被禁用到:APPNAME (com.example.pack)

需要采取的行动:检查您帐户中所有其他剩余应用程序的合规性。

当前帐户状态:活动.....

So now you need to change your package of your app. on eclipse it is package="com.example"

所以现在你需要改变你的应用程序包。在 Eclipse 上它是 package="com.example"