1

我想运行我从 这里获得的谷歌开发者示例应用程序

  1. 我已经在 admobs.com 上注册了我的广告单元 ID,其格式为:ca-app-pub-7.../... 在 main.xml 中,我随后将 AD_UNIT_ID_GOES_HERE 替换为这个新的广告单元 ID。
  2. 然后我在 main.xml TEST_DEVICE_ID_GOES_HERE 中替换了我从 logcat 获得的设备 ID:

    09-07 19:49:30.881: I/Ads(5735): To get test ads on this device, call adRequest.addTestDevice("EFEA76C9D061FDD37B8ABF6EB712A991");
    

同样在 BannerSample.java 中,我在 onCreate 中添加了代码:

package com.google.example.ads.xml;

import com.google.ads.AdRequest;

import android.app.Activity;
import android.os.Bundle;

/**
* A simple {@link Activity} which embeds an AdView in its layout XML.
*/
public class BannerSample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // This example requires no additional code since the optional
    // "loadAdOnCreate=true" XML attribute was used. If "loadAdOnCreate" were
    // not specified, the ad would have to be loaded by creating an AdRequest
    // and using Activity.findViewById() to get the AdView.
    //
    // The "loadAdOnCreate" XML attribute makes it simpler to get ads since no
    // code is required, but it also limits the developer's control over the ad
    // request since a generic AdRequest is used.

    AdRequest request = new AdRequest();
    request.addTestDevice(AdRequest.TEST_EMULATOR);
    request.addTestDevice("EFEA76C9D061FDD37B8ABF6EB712A991");

  }
}

我等了几分钟,应用程序正在运行,但没有出现广告横幅。

日志猫:

09-07 20:32:01.037: W/webcore(5945): Can't get the viewWidth after the first layout
09-07 20:33:00.306: I/Ads(5945): AdLoader timed out after 60000ms while getting the URL.
09-07 20:33:00.310: D/webviewglue(5945): nativeDestroy view: 0x1cb198
09-07 20:33:00.322: I/Ads(5945): onFailedToReceiveAd(A network error occurred.)

我也尝试过将代码添加到清单中。(我在stackoverflow上看到过,但没有奏效)。

    <meta-data
        android:name="ADMOB_PUBLISHER_ID"
        android:value="pub-7..."
        >
    </meta-data>

我正在使用最新版本的 GoogleAdMobAdsSdkAndroid-6.4.1。感谢您的任何建议。

编辑:我已经在模拟器和其他手机上尝试过我的应用程序,没有问题。广告横幅显示良好。没有出现横幅的手机是三星 I5801 和 android 2.3.7。它最初不是我的手机,它可能是 root 手机,可能是什么原因,但我不确定(我在论坛上找到了它)。

4

4 回答 4

1

尝试

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    AdView adView = (AdView) this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest();
    adView.loadAd(adRequest);
}

在主布局中添加 xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 和

<com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="YOUR_KEY"
    ads:refreshInterval="32" />

希望有帮助:)

于 2013-09-07T20:24:31.553 回答
1

您可能会忘记在您的 Android maifest 文件上添加权限。好像是这样

您的 Android 清单中需要 INTERNET 和 ACCESS_NETWORK_STATE 权限,靠近清单标签。

于 2013-09-09T11:27:33.283 回答
1

您可以在字符串中定义您的 admob id,以及所有测试设备以便于访问

<com.google.ads.AdView
    xmlns:googleads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/myAdView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    googleads:adSize="SMART_BANNER"
    googleads:adUnitId="@string/admob_id"
    googleads:testDevices="@string/ADS_TEST_DEVICES" />

然后在您的活动中:

package com.google.example.ads.xml;

import com.google.ads.AdRequest;

import android.app.Activity;
import android.os.Bundle;

/**
* A simple {@link Activity} which embeds an AdView in its layout XML.
*/
public class BannerSample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    AdView myAdView = (AdView) this.findViewById(R.id.myAdView);
    AdRequest request = new AdRequest();
    myAdView.loadAd(adRequest);
  }
}

快乐编码!

于 2013-09-08T02:31:42.357 回答
0

message:There was a problem getting an ad response. ErrorCode: 1表示广告单元 ID 无效。

public static final int ERROR_CODE_INVALID_REQUEST
The ad request was invalid; for instance, the ad unit ID was incorrect.
Constant Value: 1 (0x00000001)

解决方案是更改ad unit ID.

于 2014-09-01T22:28:14.877 回答