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

The ad size and ad unit ID must be set before loadAd is called in android

我在我的应用程序中使用 Adview。但是每当我 运行 应用程序崩溃并显示以下异常时。 MainActivity.java 是初始屏幕后的第一个屏幕。

Caused by: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.

XML

<?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:ads="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e2e2e2"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_category"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_below="@+id/toolbar_category"
        android:scrollbars="vertical" />
    <com.google.android.gms.ads.AdView

        android:id="@+id/adView_Category"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_home_footer">
    </com.google.android.gms.ads.AdView>



</RelativeLayout>

MainActivity.java

AdView ad = (AdView)findViewById(R.id.adView_Category);
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();



        ad.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
            }

            @Override
            public void onAdClosed() {
                Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdLeftApplication() {
                Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdOpened() {
                super.onAdOpened();
            }
        });

        ad.loadAd(adRequest);

谁能帮我解决这个问题?

像这样声明 AdView,

<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="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_home_footer"
ads:loadAdOnCreate="true" >

或者,我猜你需要更换

xmlns:ads="http://schemas.android.com/tools" 

通过 xmlns:ads="http://schemas.android.com/apk/res-auto" 在你的 parent RelativeLayout