我不知道为什么,但出于某种原因,在我的应用中,当我调用AdView.loadAd(AdRequest)时,我的应用输出W/Ads: Required XML attribute "adSize" was missing.
,然后崩溃
java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
即使在我的xml布局文件中,我已经定义了adSize
。
有什么建议?
XML布局文件
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout"
android:background="@android:color/white"
android:orientation="vertical"
android:padding="2dp"
android:paddingTop="10dp">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="****************" />
</LinearLayout>
Java代码
AdView ad = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
ad.loadAd(adRequest);
答案 0 :(得分:0)
在调用loadAd()
方法之前,以编程方式设置广告尺寸和横幅ID:
AdView ad = (AdView) findViewById(R.id.adView);
//Set the Ad Size
ad.setAdSize(AdSize.BANNER);
//Set the Banner Id
ad.setAdUnitId(YOUR_BANNER_ID);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
ad.loadAd(adRequest);
将您的布局更改为:
<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"
android:background="@android:color/white"
android:orientation="vertical"
android:padding="2dp"
android:paddingTop="10dp">
<RelativeLayout
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>