The ad size and ad unit ID must be set before load

2020-02-23 05:15发布

问题:

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:

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>

and

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

回答1:

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

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

My answer is:

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.

By using findViewById from your com.google.android.gms.ads.AdView

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

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

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 - 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 - The only thing programatically you can do is call mAdView.loadAd(adRequest) to load the ad

By using new AdView()

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

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

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

My conclusions are:

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

and load view on 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.



回答2:

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

This solved my problem.

do not use this

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


回答3:

After trying,

  1. Supply adunit ID and adsize from xml ONLY.
  2. Supply adunit ID and adsize from .java file ONLY.

Problem is solved by

  1. Removing xmlns:ads="http://schemas.android.com/apk/res-auto" line from root layout.
  2. supply adunit ID and adsize as follows,

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

    1. Clean and run app.


回答4:

add this to your layout

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


回答5:

You've got

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

From: 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.

ads:adUnitId="AD_UNIT_ID"


回答6:

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


回答7:

For me, I was setting the ad type like this:

ads:adSize="Banner"

While it shall be all caps:

ads:adSize="BANNER"