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