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

2022-01-25 00:00:00 android java admob adview

我不知道这里发生了什么,但我正在尝试通过如下代码动态设置我的广告单元 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:

必须在调用 loadAd 之前设置广告尺寸和广告单元 ID.

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 文件中

And in your xml file

<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),如果您想在同一屏幕上显示更多横幅(不推荐),您必须创建另一个 <来自 console 的 code>unitId 以及每个 unitId 的一个 adView.

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.

我的回答是:

不知道这是一个错误,还是每个 adView 只能有一个 unitId 更有意义,因为每个 adView 只能有一个 unitId,并从 docs 中阅读,他们展示了两种方法,通过实例化一个新的 AdView() 并以编程方式设置 unitIdssizes 或仅从 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 - 如果您先设置 adSize,则可以通过编程方式 setAdUnitId.

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

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

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 - 如果没有放置 setAdUnitIdsetSize 并以编程方式放置,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

通过使用 new AdView()

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

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

2 - 您可以通过编程方式设置 adSizeadUnitId 它将起作用.

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

3- 如果您尝试使用 setAdUnitAd 两次此异常将启动 广告单元 ID 只能在 AdView 上设置一次. 如果您使用 findViewById

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 中使用"

You can use only from 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.

相关文章