必须在调用loadAd之前设置广告尺寸和广告单元ID

时间:2014-04-28 09:50:03

标签: java android admob

我在我的主xml文件中有这个:

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

AdView adView = (AdView)this.findViewById(com.example.lovetestactual.R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);

它抛出了标题中的例外情况。

8 个答案:

答案 0 :(得分:157)

我在github示例中找到了解决方案,即:

而不是

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

删除xmlns:ads ***标记并添加

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

标记到您的LinearLayout标记,如下所示:

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

就是这样:))

github link for this xml

答案 1 :(得分:18)

警告:确保以相同的方式设置广告尺寸和广告单元ID(例如,以XML格式设置或以编程方式设置)。

https://developers.google.com/admob/android/banner

答案 2 :(得分:4)

由于您没有说明您的XML文件是什么类型的视图(相对,线性等),这将是模糊的。

在我的应用程序中,我使用了可滚动的相对布局:

<RelativeLayout
    android:id="@+id/test"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/TextView07"
    android:paddingTop="20dp" >
</RelativeLayout>

现在在我的实际课程中,我想要包含的广告:

private AdView adView;

/* Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID = "ca-app-pub-xxxxxx/yyyyyy";

/**
 * Simply loads the xml about page layout to display the text.
 */

public void onCreate(Bundle start) {
    super.onCreate(start);
    setContentView(R.layout.about);

    final TelephonyManager tm = (TelephonyManager) getBaseContext()
            .getSystemService(Context.TELEPHONY_SERVICE);

    //String deviceid = tm.getDeviceId();

    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    RelativeLayout layout = (RelativeLayout) findViewById(R.id.test);
    layout.addView(adView);

    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
    .build();
            //.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            //.addTestDevice(deviceid).build();

    // Start loading the ad in the background.
    adView.loadAd(adRequest);
}

现在理想情况下,您希望使用智能横幅来优化所有设备的布局,如上所述,这是现在的标准。

如果没有帮助,请转到here

答案 3 :(得分:4)

很多人将现有应用程序迁移到google play服务方法都错误地添加了错误的信息。

我想指出的是,许多有相同问题的人一直在使用此作为模板

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

这是不正确的,因为xmlns:ads应更改为: -     xmlns:ads="http://schemas.android.com/apk/res-auto"

喜欢这样

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

答案 4 :(得分:1)

从根视图中删除xmlns:ads="http://schemas.android.com/tools"

然后添加 AdView

<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"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>

请记住以下步骤:

  1. 您可以在自定义根视图上定义xmlns:ads="http://schemas.android.com/apk/res-auto"

  2. 如果您未在rootView中定义xmlns:ads="http://schemas.android.com/apk/res-auto",则必须在 AdView中添加xmlns:ads="http://schemas.android.com/apk/res-auto"

  3. 如果您在AdView xml文件中定义了adSize,则不要动态设置,例如:adView.setAdSize(AdSize.BANNER);,否则它将通过异常java.lang.IllegalStateException: The ad size can only be set once on AdView.

  4. xmlns:ads="http://schemas.android.com/apk/res-auto"xmlns:app="http://schemas.android.com/apk/res-auto"看起来相同,但有一些区别是 xmlns:ads xmlns:app 所以不要& #39;混淆。

  5. 我认为它会解决你的问题......乐于助人:)

答案 5 :(得分:0)

I tried changing the namespace declaration but it just doesn't work. I fixed this by specifying a height value for the adView instead of wrap_content. It works for me

<com.google.android.gms.ads.AdView
    android:id="@+id/adView2"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center|bottom"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id" />

答案 6 :(得分:0)

我遇到了同样的问题,因为我没有在的xmlns:ads="http://schemas.android.com/apk/res-auto"上添加此行。然后我像这样更改我的xml代码

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

之后,我在onCreate方法中添加这3行

AdView mAdView = (AdView)findViewById(R.id.adView);
    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

答案 7 :(得分:0)

对于任何对此感兴趣的人都是Kotlin版本。

<RelativeLayout
    android:id="@+id/adContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true">
</RelativeLayout>

在后面的代码中

//create a new AdView    
val adView = AdView(this)
adView.adSize = AdSize.SMART_BANNER
adView.adUnitId = "your adUnitID goes here or you can use a function"
adView.visibility = View.GONE

//add it to the layout
val layout = adContainer
layout.addView(adView)

//profit
MobileAds.initialize(this) {}
val adRequest = AdRequest.Builder().build()
adView.loadAd(adRequest)
adView.adListener = object : AdListener(){
    override fun onAdLoaded() {
        adView.visibility = View.VISIBLE
        super.onAdLoaded()
    }
}