必须先设置广告尺寸和广告单元ID,然后才能在Android中调用loadAd

时间:2018-02-06 04:53:24

标签: android admob adview banner-ads

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

  

引起:java.lang.IllegalStateException:广告尺寸和广告单元ID   必须在调用loadAd之前设置。

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

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

像这样声明AdView,

    firestore exports.noteCreated = functions.firestore
  .document('note/{noteId}')
  .onCreate(event => {
    // Get an object representing the document
    // e.g. {'name': 'Marie', 'age': 66}
    console.log('New Note Created');
});

或者,我想你需要更换

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