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

The ad size and ad unit ID must be set before loadAd when set programmatically

我不知道这里发生了什么,但我正在尝试通过如下代码动态设置我的广告单元 ID 并将其从 XML 中删除,但仍然出现错误:

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 来自 console 并且每个 unitId.

一个 adView

我的回答是:

不知道这是一个错误还是每个 adView 只能有一个 unitId 并且它更有意义,因为每个 adView 只能有一个 unitId,并且从docs 他们展示了两种方法,通过实例化一个新的 AdView() 并以编程方式设置 unitIdssizes OR 仅从 XML 开始。

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

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

中的 findViewById

1 - 如果您先设置 adSize,则可以通过编程方式 setAdUnitId

2 - 如果它已经在您的 xml.

中,则您不能以编程方式 setAdUnitId

3 - 如果您不在 xml 中使用“setAdUnitId”,它会提醒 Required xml attribute adUnitId was missing,即使您以编程方式设置这两个属性,也会提醒 adSize

4 - 如果不放置 setAdUnitIdsetSize 并以编程方式放置,adView 将提醒您 Required xml attribute adUnitId was missing,如果您不设置 adSize xml.

5 - 您唯一可以编程的方式是调用 mAdView.loadAd(adRequest) 加载广告

通过使用new AdView()

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

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

3- 如果您尝试使用 setAdUnitAd 两次,此异常将启动 The ad unit ID can only be set once on AdView. 如果您使用 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 上加载视图

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

我使用横幅很长时间了,这个答案对我来说很好。

你有

android:layout_width="wrap_content"
android:layout_height="wrap_content"

来自: 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.

改为

android:layout_width="match_parent"

并在 xml 文件中设置您的 adUnitID。

ads:adUnitId="AD_UNIT_ID"

对我来说,我是这样设置广告类型的:

ads:adSize="Banner"

虽然全部大写:

ads:adSize="BANNER"

我做了这样的

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

在代码中

@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);
                }
            }//
    );
}
xmlns:ads="http://schemas.android.com/apk/res-auto" 

这解决了我的问题。

不要使用这个

xmlns:ads="http://schemas.android.com/tools" 

经过尝试,

  1. 仅从 xml 提供广告单元 ID 和广告尺寸。
  2. 仅从 .java 文件提供 adunit ID 和广告尺寸。

问题已解决

  1. 正在从根布局中删除 xmlns:ads="http://schemas.android.com/apk/res-auto" 行。
  2. 按如下方式提供广告单元 ID 和广告尺寸,

    app:adSize="SMART_BANNER" app:adUnitId="@string/banner_ad_unit_id"

    1. 清洁和 运行 应用程序。

将此添加到您的布局中

xmlns:ads="http://schemas.android.com/apk/res-auto"