[英]Unable to integrate AdMob ads in my Android app
我一直在按照AdMob網站(此處為https://developers.google.com/mobile-ads-sdk/docs/?hl=zh-CN)上的說明進行操作,但仍然無法正常投放廣告,但似乎沒有什么錯。
這是我的布局:
<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均已正確添加到代碼中。
您正在使用兩個單獨的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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.