1

I am using Adview in my app. But when everytime when I run app got crash and shows following exception. MainActivity.java is first screen after splash screen.

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

Can any one help me to solve this?

Dhaval Asodariya
  • 558
  • 5
  • 19
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96

1 Answers1

3

Declare AdView like this,

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

Or, I guess you need to replace

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

By xmlns:ads="http://schemas.android.com/apk/res-auto" inside your parent RelativeLayout

Exigente05
  • 2,161
  • 3
  • 22
  • 42
  • right now it is showing error code 0 – Aditya Vyas-Lakhan Feb 06 '18 at 05:28
  • That's good. Error code 0 means currently you are not getting ads. This could happen if you request with newly created ID. It will start showing ads automatically after some hours. You have nothing to do but you can test with dummy unit ids - https://stackoverflow.com/questions/12553929/is-there-any-admob-dummy-id Hope my answer helps you. :) – Exigente05 Feb 06 '18 at 05:42
  • yes right now its not crashing, but can you tell after how many hours it will show my ads. yesterday also it was showing error code 0. ad was not loading – Aditya Vyas-Lakhan Feb 06 '18 at 06:17
  • Here you need to ensure something, adtype (adSize) where you are putting ad unit id should be same of adType you selected while creating ID. There is no exact time, it starts showing after some good request. Have a look these topics - https://groups.google.com/forum/#!topic/google-admob-ads-sdk/fBe3YL3ffpo%5B101-125%5D – Exigente05 Feb 06 '18 at 06:23