【问题标题】:The ad size and ad unit ID must be set before loadAd is called. when I set the banner programmatically必须在调用 loadAd 之前设置广告尺寸和广告单元 ID。当我以编程方式设置横幅时
【发布时间】:2017-11-06 13:22:26
【问题描述】:

我面临 java.lang.IllegalStateException:必须在调用 loadAd 之前设置广告尺寸和广告单元 ID。每当我尝试以编程方式设置横幅时。事实上,我想从 java 类而不是 xml 中设置 AdUnitID。

这里是活动

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.test">

**SOME COMPONENTS**

        <RelativeLayout
            android:id="@+id/adMobView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true">

        </RelativeLayout>


</RelativeLayout>

这里是 java 类(oncreate)

RelativeLayout adContainer = (RelativeLayout)findViewById(R.id.adMobView);
            AdView mAdView = new AdView(this);
            mAdView.setAdSize(AdSize.SMART_BANNER);
            mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
            adContainer.addView(mAdView);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);

清单

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Gradle

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.google.android.gms:play-services-ads:11.4.2'
}

【问题讨论】:

  • 为什么要添加运行时为什么不在 xml 中?

标签: java android admob banner


【解决方案1】:

我想您的 AdMob 的 XML 代码不正确。确保将 AdMob 的 xmlns 添加到您的根目录 &lt;RelativeLayout&gt;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ads="http://schemas.android.com/apk/res-auto" //This line
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

如果您已经添加了上述代码,那么您需要以编程方式创建它。

在您的活动 java 文件中:

View adContainer = findViewById(R.id.adMobView);
AdView mAdView = new AdView(context);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(YOUR_BANNER_ID);
((RelativeLayout)adContainer).addView(mAdView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

在您的 XML 文件中使用它:

<RelativeLayout 
android:id="@+id/adMobView"
android:layout_width="match_parent"
android:layout_height="match_parent
android:layout_alignParentBottom="true"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多