横幅广告错误:广告大小和广告单位ID必须在loadAd被调用之前设置



这不是一个重复问题。我已经尝试过类似问题的解决方案,但没有一个奏效。

我的谷歌广告横幅代码如下:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/konstraitLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:padding="0dp"
app:layoutDescription="@xml/activity_main_scene"
tools:context=".MainActivity">
...
<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:adSize="Banner"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
...
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity中的代码:

adView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);

所以我在XML中添加了我的横幅。我在java中找到它,然后调用它,但我想我错过了一些东西,因为我有以下错误:

广告大小和广告单位ID必须在loadAd调用前设置

谢谢你的回答。

adSize标签的值应该用大写字母传递。替换:

ads:adSize="Banner"

与这个:

ads:adSize="BANNER"

最新更新