Android - Admob测试广告没有出现在应用中

时间:2014-03-17 11:22:20

标签: java android eclipse admob banner

我想将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>

layout - 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(使用Google的示例测试代码的类)

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();

  }
}

2 个答案:

答案 0 :(得分:0)

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

在AdBanner.java中,您可以使用:

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

但在您的布局中使用:

<com.google.ads.AdView

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

答案 1 :(得分:0)

我认为你必须检查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" />

最好的问候。

相关问题