【问题标题】:Android - Admob test ads not appearing in appAndroid - Admob 测试广告未出现在应用中
【发布时间】:2014-03-17 11:22:20
【问题描述】:

我想将 Google 移动广告集成到我的应用中,但是当我调试它时,无论何时使用我自己的设备或使用模拟器,都不会出现广告。

我正在使用来自 Google 开发者页面的代码:https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals

查看 logcat 时,我发现我的应用没有请求任何广告,而 Admob 报告没有请求。

我错过了什么?

这是我的代码

AndroidManifest

<application android:icon="@drawable/ic_launcher">
    <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version"/>
    android:label="@string/app_name" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     <activity android:name="com.google.android.gms.ads.AdActivity"
         android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

</manifest>

布局 - main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/adBanner"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
  <com.google.ads.AdView android:id="@+id/ad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adUnitId="-omitted-"
    ads:adSize="BANNER" />
</LinearLayout> 

AdBanner.java(带有谷歌示例测试代码的类)

package com.google.android.gms.ads.banner;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

import -omitted-
import android.os.Bundle;
import android.widget.LinearLayout;
import -omitted-


public class AdBanner extends MainActivity{
/**
 * A simple {@link Activity} that embeds an AdView.
 */
  /** The view to show the ad. */
  private AdView adView;

  /* Your ad unit id. Replace with your actual ad unit id. */
  private static final String AD_UNIT_ID = "-omitted-";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Create an ad.
    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    LinearLayout layout = (LinearLayout) findViewById(R.id.adBanner);
    layout.addView(adView);

    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("-omitted-")
        .build();

    // Start loading the ad in the background.
    adView.loadAd(adRequest);
  }

  @Override
  public void onResume() {
    super.onResume();
    if (adView != null) {
      adView.resume();
    }
  }

  @Override
  public void onPause() {
    if (adView != null) {
      adView.pause();
    }
    super.onPause();
  }

  /** Called before the activity is destroyed. */
  @Override
  public void onDestroy() {
    // Destroy the AdView.
    if (adView != null) {
      adView.destroy();
    }
    super.onDestroy();

  }
}

【问题讨论】:

    标签: java android eclipse admob banner


    【解决方案1】:

    我在你的代码中看到了一些奇怪的东西:

    在您的 AdBanner.java 中使用:

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

    但在您使用的布局中:

    <com.google.ads.AdView
    

    对应于旧的 Admob,但您可能希望在布局中使用 com.google.android.gms.ads.AdView

    【讨论】:

    • 我明白了。我按照你的建议做了,但是我仍然没有收到任何请求或印象。我在想我的应用程序可能正在绘制横幅。但我只对所有图形使用了 .png 文件,而且我认为 google 给我的代码在任何应用程序的整个操作上都贴了一个横幅。还有什么我可能做错的吗?
    • 现在我刚刚看到 AdBanner extends MainActivity 没有意义:将所有代码从 AdBanner 移动到主要活动并删除这个 AdBanner 类。
    • 好吧,现在横幅出现了,但现在它给了我一个错误(缺少 XML 属性 AdSize),我似乎不明白,因为 adSize 是在两个布局 xml 中定义的和班级。此外,我的应用程序不再运行,但我假设我必须在下一个关于谷歌开发人员的教程中进一步自定义我的横幅代码,以使应用程序和横幅都能正常工作。
    • 你必须使用 xmlns:ads="schemas.android.com/apk/res-auto" 而不是 xmlns:ads="schemas.android.com/apk/lib/com.google.ads" 检查我的答案
    【解决方案2】:

    我认为您必须检查 android 日志以查看是否为您的 admob 提供了任何错误或警告。例如:我也看不到广告,当我检查日志时,我发现我的广告出现“空间不足”警告。我使用的尺寸“SMART_BANNER”使用宽度为 360dp(您可以检查尺寸here)。所以我将 AdView 的宽度更改为精确的 360dp 以解决我的问题,现在它正在工作:Stil

    我希望这些信息对您有所帮助。只需检查日志和控制台。

    另外,我的adView:

    <com.google.android.gms.ads.AdView
                xmlns:ads="http://schemas.android.com/apk/res-auto"
                android:id="@+id/adView"
                android:layout_width="360dp"
                android:layout_height="wrap_content"
                ads:adSize="SMART_BANNER"
                ads:adUnitId="@string/banner_ad_unit_id" />
    

    最好的问候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多