代码之家  ›  专栏  ›  技术社区  ›  Aditya Vyas-Lakhan user1796260

在android中调用loadAd之前,必须设置广告大小和广告单元ID[重复]

  •  1
  • Aditya Vyas-Lakhan user1796260  · 技术社区  · 7 年前

    我正在应用程序中使用Adview。但每次我运行应用程序时都会崩溃,并显示以下异常。主要活动。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>
    

    主要活动。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 回复  |  直到 7 年前
        1
  •  3
  •   Exigente05    7 年前

    像这样声明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" 在您的家长内部 RelativeLayout