0

I am having problems adding banner ads in my android app. I just copied the code off the documentation from the website, I can seem to figure out what I am not doing. The app keeps crashing. I have the code below in Mainactivity.java

private AdView mAdView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });

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

And my Manifest looks as shown below

<activity
            android:name=".MainActivity"
            android:exported="true"
            android:theme="@style/AppTheme.Launcher">
            <meta-data
                android:name="preloaded_fonts"
                android:resource="@array/preloaded_fonts" />
            <meta-data
                android:name="com.google.android.gms.ads.APPLICATION_ID"
                android:value="ca-app-pub-admod_generated number"/>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Code for the page where the ad appears look as shown below

<androidx.constraintlayout.widget.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/FlashcardimageView"
        android:layout_width="326dp"
        android:layout_height="290dp"
        android:layout_marginBottom="32dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.506"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.45" />

    <TextView
        android:id="@+id/FlashcardTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="16dp"
        android:text="Mark"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textSize="50sp"
        app:fontFamily="@font/atma_semibold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.544"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.853" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="44dp"
        android:layout_marginRight="44dp"
        android:layout_marginBottom="28dp"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"></com.google.android.gms.ads.AdView>


</androidx.constraintlayout.widget.ConstraintLayout>

Error message from Debug terminal is as shown below

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.xtenalyze.android.myapplication, PID: 15281
    java.lang.RuntimeException: Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException: 
    
    ******************************************************************************
    * The Google Mobile Ads SDK was initialized incorrectly. AdMob publishers    *
    * should follow the instructions here:                                       *
    * https://googlemobileadssdk.page.link/admob-android-update-manifest         *
    * to add a valid App ID inside the AndroidManifest.                          *
    * Google Ad Manager publishers should follow instructions here:              *
    * https://googlemobileadssdk.page.link/ad-manager-android-update-manifest.   *
    ******************************************************************************
    
    
        at android.app.ActivityThread.installProvider(ActivityThread.java:7828)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:7364)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7250)
        at android.app.ActivityThread.access$1600(ActivityThread.java:293)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2151)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:263)
        at android.app.ActivityThread.main(ActivityThread.java:8283)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)
     Caused by: java.lang.IllegalStateException: type here

Can anyone look into the code and offer assistance? Thanking everyone for their time and offer to help.

A review of my code, hopefully to find errors

Tyler V
  • 9,694
  • 3
  • 26
  • 52
Mxgrig
  • 1
  • 1

1 Answers1

0

It looks like you put the ID in the wrong place in your manifest. Per the documentation linked in that error message, it should go at the application level, not the activity level. You should change

<application>
  <activity>
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-admod_generated number"/>
    // other stuff
  </activity>
</application>

to this

<application>
  <meta-data
      android:name="com.google.android.gms.ads.APPLICATION_ID"
      android:value="ca-app-pub-admod_generated number"/>

  <activity>
    // other stuff
  </activity>
</application>
Tyler V
  • 9,694
  • 3
  • 26
  • 52