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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23337802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:00:59  来源:igfitidea点击:

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

javaandroidadmob

提问by zbz.lvlv

I have this in my main xml file:

我的主 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"/>

I have already set the ad size and unit id, but when this is run (from MainActivity.java),

我已经设置了广告尺寸和单元 ID,但是当它运行时(来自 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.

它抛出了标题中的异常。

采纳答案by Hamed MP

I found solution in the github example, that is:

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

instead of

代替

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

delete the xmlns:ads*** tag and add

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

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

tag to your LinearLayout tag as follow:

标记到您的 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>

That's it :)

就是这样 :)

github link for this xml

这个 xml 的 github 链接

回答by furkick

This is going to be vague as you have not stated what type of view your XML file is (Relative, Linear etc.)

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

In my application with a scrollable relative layout I have included:

在我的具有可滚动相对布局的应用程序中,我包含了:

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

Now inside my actual class that I wan't the ad I have included:

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

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

Now ideally you want to use a smart banner to optimise your layout for all devices, as I have above, this is standard now.

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

If this is no help head over to here

如果这没有帮助,请转到此处

回答by Premier Invites

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

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

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"

这是不正确的,因为 xmlns:ads 应该更改为:- 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>

回答by Kelok Chan

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

我尝试更改命名空间声明,但它不起作用。我通过为 adView 指定高度值而不是wrap_content. 这个对我有用

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

回答by Dan Alboteanu

Warning:Make sure you set the ad size and ad unit ID in the same manner (i.e. set both in XML or both programmatically).

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

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

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

回答by Abdul Rizwan

remove xmlns:ads="http://schemas.android.com/tools"from root view

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

then add AdView

然后添加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>

Remember these steps:

记住这些步骤:

  1. You can define xmlns:ads="http://schemas.android.com/apk/res-auto" on root view it self .

  2. if you have not defined xmlns:ads="http://schemas.android.com/apk/res-auto"inside rootView then you must need to add xmlns:ads="http://schemas.android.com/apk/res-auto"inside AdView.

  3. if you have defined adSize inside your AdViewxml file then don't set dynamically, like : adView.setAdSize(AdSize.BANNER);or else it will through an exception java.lang.IllegalStateException: The ad size can only be set once on AdView.

  4. xmlns:ads="http://schemas.android.com/apk/res-auto"and xmlns:app="http://schemas.android.com/apk/res-auto"are looking same but there is a difference which is xmlns:adsand xmlns:appso don't get confuse.

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

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

  3. 如果您在AdViewxml 文件中定义了 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:adsxmlns:app所以不要混淆。

I think it will resolve your issue ... Happy to help :)

我认为它会解决您的问题......很乐意提供帮助:)

回答by DEVSHK

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

我遇到了同样的问题,因为我没有在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>

after that i add this 3 line in onCreate method

之后我在 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);