Ad Size and Ad unit id must be set before loadAd is called

后端 未结 8 1469
后悔当初
后悔当初 2020-12-03 00:52

I have this in my main xml file:



        
相关标签:
8条回答
  • 2020-12-03 01:19

    A lot of people Migrating existing applications to the google play services method are incorrectly adding the wrong information.

    Something that I would like to point out is that many people with the same question have been using this as a template

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/lib-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXX"
        ads:loadAdOnCreate="true" >
    </com.google.android.gms.ads.AdView>
    

    This is incorrect as xmlns:ads should be change to:- xmlns:ads="http://schemas.android.com/apk/res-auto"

    Like so

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXX"
        ads:loadAdOnCreate="true" >
    </com.google.android.gms.ads.AdView>
    
    0 讨论(0)
  • 2020-12-03 01:22

    I faced that same problem because i do not add this line on xmlns:ads="http://schemas.android.com/apk/res-auto" top of . Then i change my xml code like this

        <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:layout_constraintLeft_toLeftOf="parent"
        ads:layout_constraintRight_toRightOf="parent"
        ads:layout_constraintTop_toTopOf="parent"
        ads:layout_constraintBottom_toTopOf="@+id/headerText"
        android:layout_marginTop="@dimen/_2sdp"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        android:visibility="visible">
    </com.google.android.gms.ads.AdView>
    

    after that i add this 3 line in onCreate method

    AdView mAdView = (AdView)findViewById(R.id.adView);
        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    
    0 讨论(0)
提交回复
热议问题