Ad Size and Ad unit id must be set before loadAd is called(必须在调用 loadAd 之前设置广告尺寸和广告单元 ID)
问题描述
我的主 xml 文件中有这个:
I have this in my main xml file:
<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adUnitId="******************"
        ads:adSize="BANNER"
        android:id="@+id/adView"/>
我已经设置了广告尺寸和单元 ID,但是当它运行时(来自 MainActivity.java),
I have already set the ad size and unit id, but when this is run (from MainActivity.java),
AdView adView = (AdView)this.findViewById(com.example.lovetestactual.R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
它抛出了标题中的异常.
It threw an exception of what is in the title.
推荐答案
我在github例子中找到了解决方案,即:
I found solution in the github example, that is:
而不是
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
删除 xmlns:ads*** 标签并添加
delete the xmlns:ads*** tag and add
xmlns:ads="http://schemas.android.com/apk/res-auto"
标记到您的 LinearLayout 标记如下:
tag to your LinearLayout tag as follow:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ads="http://schemas.android.com/apk/res-auto"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
<TextView android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="@string/hello"/>
<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="INSERT_YOUR_AD_UNIT_ID_HERE"/>
</LinearLayout>
就是这样:)
此xml的github链接
这篇关于必须在调用 loadAd 之前设置广告尺寸和广告单元 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:必须在调用 loadAd 之前设置广告尺寸和广告单元 ID
				
        
 
            
        - 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
 - 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
 - 转换 ldap 日期 2022-01-01
 - 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
 - GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
 - Eclipse 的最佳 XML 编辑器 2022-01-01
 - 未找到/usr/local/lib 中的库 2022-01-01
 - 如何指定 CORS 的响应标头? 2022-01-01
 - 获取数字的最后一位 2022-01-01
 - java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
 
						
						
						
						
						
				
				
				
				