-2

I am working on simple android app that contain Admob banner, but when I run the app I get this message:

unfortunately the app has stopped

here is my code:

xml layout:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.ajater.mj.vaccalc.MainActivity"
xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads">


   <com.google.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:loadAdOnCreate="true"
    ads:adSize="BANNER"
    ads:adUnitId="------------------" />
   </android.support.design.widget.CoordinatorLayout>

mainactivity

    AdView adView = (AdView)findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

AndroidManifest

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.app.name">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

2 Answers2

0

I think you need to add this line above your AdView

 MobileAds.initialize(getApplicationContext(), "Your own admob ad-unit");

Note: Don't test your real admob ad-unit. If so, There is chance to terminate your admob account by google.

Source: https://developers.google.com/admob/android/quick-start

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
0

Try this code,

<com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_key">
    </com.google.android.gms.ads.AdView>
Pratik18
  • 365
  • 1
  • 7