如何以编程指定adUnitId的Admob的 [英] How to specify adUnitId programatically for Admob
问题描述
我想设置adUnitId编程从新的谷歌游戏服务的广告(AdMob的老)。
I'm trying to set adUnitId programatically to ads from the new google play services ( old admob ).
我有这样的XML(在使用):
I have this in xml (used in an ):
<com.google.android.gms.ads.AdView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"/>
和这的onCreate():
and this in onCreate():
AdView mAdview = (AdView)findViewById(R.id.adView);
mAdview.setAdUnitId(((App)getApplication()).getAdmobKey());
mAdview.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
findViewById(R.id.adView).setVisibility(View.VISIBLE);
}
});
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mAdview.loadAd(adRequest);
和我得到:的广告尺寸和广告单元ID loadAd之前,必须设置被称为所以第二种选择,是让广告编程。
And I get: The ad size and ad unit ID must be set before loadAd is called. So the second option, was to make the ad programatically.
新的XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/adView"
/>
新的code:
the new code:
AdView mAdview = new AdView(this);
...
((LinearLayout)findViewById(R.id.adView)).addView(mAdview);
mAdview.loadAd(adRequest);
但同样的错误......我也尝试从com.google.android.gms.ads.AdView继承使一个自定义的视图,但它最终的决定。
But the same error... I tried also to inherit from com.google.android.gms.ads.AdView to make an custom View, but it's final.
任何建议?谢谢!
推荐答案
方法 loadAd()
检查是否(mAdView.getAdSize()= = NULL || mAdView.getAdUnitID()== NULL)
时loadAd发生。
The method loadAd()
checks if (mAdView.getAdSize() == null || mAdView.getAdUnitID() == null)
when loadAd happens.
尝试登录的布尔输出(mAdView.getAdSize()== NULL || mAdView.getAdUnitID()== NULL)
要求loadAd以确定其状态之前:
Try logging the boolean output of (mAdView.getAdSize() == null || mAdView.getAdUnitID() == null)
before calling loadAd to determine its state:
mAdView = new AdView(this);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(AD_UNIT_ID);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
if(mAdView.getAdSize() != null || mAdView.getAdUnitID() != null)
mAdView.loadAd(adRequest);
// else Log state of adsize/adunit
((LinearLayout)findViewById(R.id.adView)).addView(mAdview);
这篇关于如何以编程指定adUnitId的Admob的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!