Java 以编程方式设置时,必须在 loadAd 之前设置广告尺寸和广告单元 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34231810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The ad size and ad unit ID must be set before loadAd when set programmatically
提问by Mike Flynn
I have no idea what is going on here but I am trying to set my ad unit ID dynamically through code like below and removing it from the XML but still get the error:
我不知道这里发生了什么,但我正在尝试通过如下代码动态设置我的广告单元 ID 并将其从 XML 中删除,但仍然出现错误:
The ad size and ad unit ID must be set before loadAd is called.
必须在调用 loadAd 之前设置广告尺寸和广告单元 ID。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER">
</com.google.android.gms.ads.AdView>
and
和
AdView mAdView = (AdView) rootView.findViewById(R.id.adView);
mAdView.setAdUnitId(getEvent().getAdMobUnitId());
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
采纳答案by diogojme
Create it programatically
以编程方式创建
View adContainer = findViewById(R.id.adMobView);
AdView mAdView = new AdView(context);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(YOUR_BANNER_ID);
((RelativeLayout)adContainer).addView(mAdView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
And in your xml file
在你的 xml 文件中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/adMobView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
EDIT
编辑
The best practice for banners, is two show one banner per view (one unitId), if you want to show more banners in the same screens (NOT RECOMMENDED), you have to create another unitIdfrom consoleand one adView for each unitId.
横幅的最佳实践,在两个节目按次(一个一个旗帜unitId),如果你想显示在同一个屏幕(不推荐)更多的旗帜,你必须创建另一个unitId从控制台,并为每一个AdView的unitId。
My answer is:
我的回答是:
Don't know if its a bug or if you can have only one unitIdper adView and it make more sense, because you can only have one unitIdper adView, and reading from docsthey show two ways to do it, by instantianting a new AdView()and programtically setting the unitIdsand sizesOR
do it only from XML.
不知道这是一个错误,还是unitId每个 adView 只能有一个,这更有意义,因为unitId每个 adView只能有一个,并且从文档中阅读 它们显示了两种方法,通过实例化一个新的AdView()和以编程方式设置unitIds和sizesOR 仅从 XML 执行此操作。
And I did some tests to arrive at this conclusion.
我做了一些测试来得出这个结论。
By using findViewById from your com.google.android.gms.ads.AdView
通过使用 findViewById 从您的 com.google.android.gms.ads.AdView
1 - You can setAdUnitIdprogramatically if you set adSizefirst.
1 -setAdUnitId如果您adSize先设置,则可以通过编程方式进行设置。
2 - You cannot setAdUnitIdprogramatically if it's already in your xml.
2 -setAdUnitId如果它已经在你的 xml 中,你不能以编程方式。
3 - If you doesn't use 'setAdUnitId' in your xml, it will alert Required xml attribute adUnitId was missing, and the same for adSizeeven if you set both attributes programatically.
3 - 如果您不在 xml 中使用“setAdUnitId”,它会发出警报Required xml attribute adUnitId was missing,adSize即使您以编程方式设置这两个属性也是如此。
4 - If not put setAdUnitIdand setSizeand put it programtically, the adView will alert you Required xml attribute adUnitId was missing, and the same if you not set adSizein xml.
4 - 如果不以编程方式放置setAdUnitId和setSize放置,则 adView 会提醒您 Required xml attribute adUnitId was missing,如果您未adSize在 xml 中设置,则相同。
5 - The only thing programatically you can do is call mAdView.loadAd(adRequest)to load the ad
5 - 您唯一能mAdView.loadAd(adRequest)以编程方式做的事情就是调用加载广告
By using new AdView()
通过使用 new AdView()
1 - It will work if you create an empty layout, then add the adView reference to this view.
1 - 如果您创建一个空布局,然后将 adView 引用添加到此视图,它将起作用。
2 - You can set the adSizeand adUnitIdprogramatically it will work.
2 - 您可以设置adSize并以adUnitId编程方式工作。
3- If you try to use setAdUnitAdtwice this exception will launched The ad unit ID can only be set once on AdView.the same if you use by findViewById
3-如果您尝试使用setAdUnitAd两次此异常将启动The ad unit ID can only be set once on AdView.相同的如果您使用findViewById
My conclusions are:
我的结论是:
You can use only from XML"
您只能从 XML 使用”
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-my_id_number_was_ommited_by_security" />
and load view on onCreate
并在 onCreate 上加载视图
AdView mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
or full programatically
或完全编程
View adContainer = findViewById(R.id.adMobView);
AdView mAdView = new AdView(context);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(YOUR_BANNER_ID);
adContainer.addView(mAdView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
I use banners for a long time and that answer is good for me.
我使用横幅很长一段时间,这个答案对我有好处。
回答by Morrison Chang
You've got
你有
android:layout_width="wrap_content"
android:layout_height="wrap_content"
From: https://developers.google.com/admob/android/banner#smart_banners
来自:https: //developers.google.com/admob/android/banner#smart_banners
Note: The smart banner view in your layout must consume the full width of the device. If it doesn't, you'll get a warning with the message "Not enough space to show ad", and the banner will not be displayed.
注意:布局中的智能横幅视图必须占用设备的整个宽度。如果没有,您将收到一条警告消息“没有足够的空间来显示广告”,并且不会显示横幅。
change it to
将其更改为
android:layout_width="match_parent"
And set your adUnitID in the xml file.
并在 xml 文件中设置您的 adUnitID。
ads:adUnitId="AD_UNIT_ID"
回答by Abdalrahman Shatou
For me, I was setting the ad type like this:
对我来说,我是这样设置广告类型的:
ads:adSize="Banner"
While it shall be all caps:
虽然它应该全部大写:
ads:adSize="BANNER"
回答by Aleksey Timoshchenko
I have made it like this
我已经这样做了
<LinearLayout xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_main_screen_container"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
</LinearLayout>
in code
在代码中
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.newone);
initAdsAccount();
initAds();
}
private void initAdsAccount()
{
String accountId = AdsConstants.my_fake_ads_account_id;
if (AdsConstants.isAdsMode)
{
accountId = AdsConstants.my_ads_account_id;
}
MobileAds.initialize(this, accountId);
}
private void initAds()
{
findViewById(R.id.ll_main_screen_container).post(//
new Runnable()
{
@Override
public void run()
{
LinearLayout adContainer = findViewById(R.id.ll_main_screen_container);
AdView mAdView = new AdView(MainActivity.this);
mAdView.setAdSize(AdSize.BANNER);
if (AdsConstants.isAdsMode)
{
mAdView.setAdUnitId(AdsConstants.main_screen_bottom_banner_id);
}
else
{
mAdView.setAdUnitId(AdsConstants.fake_banner_id);
}
adContainer.addView(mAdView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
}//
);
}
回答by anwar alam
xmlns:ads="http://schemas.android.com/apk/res-auto"
This solved my problem.
这解决了我的问题。
do not use this
不要使用这个
xmlns:ads="http://schemas.android.com/tools"
回答by Shweta Bhagat
After trying,
尝试之后,
- Supply adunit ID and adsize from xml ONLY.
- Supply adunit ID and adsize from .java file ONLY.
- 仅从 xml 提供 adunit ID 和 adsize。
- 仅从 .java 文件提供 adunit ID 和 adsize。
Problem is solved by
问题解决了
- Removing
xmlns:ads="http://schemas.android.com/apk/res-auto"line from root layout. supply adunit ID and adsize as follows,
app:adSize="SMART_BANNER" app:adUnitId="@string/banner_ad_unit_id"- Clean and run app.
xmlns:ads="http://schemas.android.com/apk/res-auto"从根布局中删除线。提供 adunit ID 和 adsize 如下,
app:adSize="SMART_BANNER" app:adUnitId="@string/banner_ad_unit_id"- 清理并运行应用程序。
回答by Null Pointer Exception
add this to your layout
将此添加到您的布局
xmlns:ads="http://schemas.android.com/apk/res-auto"

