我想运行一个谷歌开发者示例应用程序,我从中获得 here
然后我在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");
}
}
我等了几分钟,App正在运行,但广告横幅没有出现。
Logcat:
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。它原本不是我的手机,它可能是根植电话可能是什么原因,但我不确定(我在论坛上找到了它。)
答案 0 :(得分: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:广告= “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" />
希望有所帮助:)
答案 1 :(得分: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);
}
}
快乐的编码!
答案 2 :(得分:1)
您可能忘记在Android maifest文件中添加权限。好像那样
INTERNET和 您的Android清单中的ACCESS_NETWORK_STATE权限更接近您的清单标记。
答案 3 :(得分:0)
消息: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
。