我一直在按照AdMob网站上的说明(此处:https://developers.google.com/mobile-ads-sdk/docs/?hl = en),我仍然无法获取广告工作,但似乎没有任何错误。
这是我的布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:soundEffectsEnabled="true"
android:id="@+id/mainLayout"
android:background="@drawable/bg_maple"
>
[...]
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adUnitId="my real unit id from admob"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, my real device id"
ads:loadAdOnCreate="true"/>
[...]
在我的活动中:
public class Banner extends Activity {
private AdView adView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the adView
adView = new AdView(this, AdSize.BANNER, "my real unit id from admob");
// Lookup your LinearLayout assuming it's been given
// the attribute android:id="@+id/mainLayout"
RelativeLayout layout = (RelativeLayout)findViewById(R.id.mainLayout);
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adRequest.addTestDevice("Here I have my device ID");
}
@Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
感谢任何帮助,我被困住了,目前似乎无法看清楚什么是错误的。
注意:我的单位ID和设备ID都正确添加到代码中。
答案 0 :(得分:1)
您正在使用两个单独的AdView,第一个来自您的xml,第二个来自您的代码。请尝试更改您的代码
public class Banner extends Activity {
private AdView adView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the adView
adView = (AdView)this.findViewById(R.id.adView);
// Lookup your LinearLayout assuming it's been given
// the attribute android:id="@+id/mainLayout"
// RelativeLayout layout = (RelativeLayout)findViewById(R.id.mainLayout);
// Add the adView to it
//layout.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adRequest.addTestDevice("Here I have my device ID");
}
@Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}