以编程方式设置时,必须在loadAd之前设置广告尺寸和广告单元ID

问题描述:

我不知道这里发生了什么,但我试图通过下面的代码动态设置我的广告单元ID并将其从XML中删除但仍然会收到错误:

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必须在调用loadAd之前设置。

The ad size and ad unit ID must be set before loadAd is called.



<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>

    AdView mAdView = (AdView) rootView.findViewById(R.id.adView);
    mAdView.setAdUnitId(getEvent().getAdMobUnitId());
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);


以编程方式创建

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);

并在您的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>

编辑

横幅的最佳做法是每个视图两个显示一个横幅(一个 unitId ),如果你想在同一个屏幕上显示更多横幅(不推荐),你必须从控制台创建另一个 unitId ,并为每个创建一个adView unitId

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 unitId from console and one adView for each unitId.

我的回答是:

Don不知道它是一个bug还是每个adView只能有一个 unitId 而且它更有意义,因为你只能有一个 unitId 每个adView,并且从文档中读取它们会显示两种方法它,通过实例化一个新的 AdView()并以编程方式设置 unitIds 尺寸 OR
仅从XML执行。

Don’t know if its a bug or if you can have only one unitId per adView and it make more sense, because you can only have one unitId per adView, and reading from docs they show two ways to do it, by instantianting a new AdView() and programtically setting the unitIds and sizes OR do it only from XML.

我做了一些测试来得出这个结论。

And I did some tests to arrive at this conclusion.

使用 com.google.android.gms.ads.AdView

1 - 您可以 setAdUnitId 以编程方式首先设置 adSize

1 - You can setAdUnitId programatically if you set adSize first.

2 - 你不能 setAdUnitId 以编程方式,如果它已经在你的xml中。

2 - You cannot setAdUnitIdprogramatically if it’s already in your xml.

3 - 如果你没有在你的xml中使用'setAdUnitId',它将提醒必需的xml属性adUnitId缺失,即使您以编程方式设置这两个属性, adSize 也是如此。

3 - If you doesn’t use ’setAdUnitId’ in your xml, it will alert Required xml attribute adUnitId was missing, and the same for adSize even if you set both attributes programatically.

4 - 如果没有放入 setAdUnitId setSize 并以程序方式放置,adView将会提醒您必需的xml属性adUnitId缺失,如果您未在xml中设置 adSize ,则相同。

4 - If not put setAdUnitId and setSize and put it programtically, the adView will alert you Required xml attribute adUnitId was missing, and the same if you not set adSize in xml.

5 - 您可以编程的唯一方法是致电 mAdView.loadAd(adRequest)加载广告

5 - The only thing programatically you can do is call mAdView.loadAd(adRequest) to load the ad

使用新的AdView()

1 - 如果您创建了一个空布局,然后将adView引用添加到此视图。

1 - It will work if you create an empty layout, then add the adView reference to this view.

2 - 您可以设置 adSize adUnitId 以编程方式运行。

2 - You can set the adSize and adUnitId programatically it will work.

3-如果您尝试使用 setAdUnitAd 此异常将启动两次广告单元ID只能在AdView上设置一次。如果您使用 findViewById $ c,则相同$ c>

3- If you try to use setAdUnitAd twice this exception will launched The ad unit ID can only be set once on AdView. the same if you use by findViewById

我的结论是:

您只能使用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" />

并在onCreate上加载视图

and load view on onCreate

AdView mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

或以编程方式完整

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.