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

zbz*_*vlv 45 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"/>
Run Code Online (Sandbox Code Playgroud)

我已经设置了广告尺寸和单位ID,但是当它运行时(来自MainActivity.java),

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

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

Ham*_* MP 161

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

代替

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
Run Code Online (Sandbox Code Playgroud)

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

xmlns:ads="http://schemas.android.com/apk/res-auto"
Run Code Online (Sandbox Code Playgroud)

标记为您的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>
Run Code Online (Sandbox Code Playgroud)

而已 :)

这个xml的github链接

  • 之前我做过相反的事情.我不认为我会再次重做它.谢谢 :) (3认同)

Dan*_*anu 20

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

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

  • 这就是我的问题.我从代码中设置了ID,并从XML中设置了大小. (3认同)

fur*_*ick 5

由于您尚未说明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>
Run Code Online (Sandbox Code Playgroud)

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

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);
}
Run Code Online (Sandbox Code Playgroud)

现在,理想情况下,您希望使用智能横幅为所有设备优化布局,就像我上面提到的那样,这是现在的标准。

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