[Android Q&A, Help & Troubleshooting] Horatio505:
I'm trying to implement interstitial ads on my google play app, but when I connect my phone and run the app, the test ad only appears for a split second before crashing. I'm a beginner and have little experience with coding so I have no idea what went wrong I might've done wrong:

This is my manifest xml:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.horatiosinterstitual">

<application

android:allowBackup="true"

android:icon="@Mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@Mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/Theme.HoratiosInterstitual">

<activity

android:name=".MainActivity"

android:exported="true">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

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

</intent-filter>

</activity>

<meta-data

android:name="com.google.android.gms.ads.APPLICATION_ID"

android:value="ca-app-pub-3426143817353439~5880284582"/>

</application>

</manifest>

Gradle file:

plugins {

id 'com.android.application'

}

android {

compileSdk 32

defaultConfig {

applicationId "com.example.horatiosinterstitual"

minSdk 21

targetSdk 32

versionCode 1

versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

}

}

compileOptions {

sourceCompatibility JavaVersion.VERSION_1_8

targetCompatibility JavaVersion.VERSION_1_8

}

}

dependencies {

implementation 'androidx.appcompat:appcompat:1.4.1'

implementation 'com.google.android.material:material:1.5.0'

implementation 'androidx.constraintlayout:constraintlayout:2.1.3'

implementation 'com.google.android.gms:play-services-ads:20.5.0'

testImplementation 'junit:junit:4.13.2'

androidTestImplementation 'androidx.test.ext:junit:1.1.3'

androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

MainActivity:

package com.example.horatiosinterstitual;

import static android.content.ContentValues.TAG;

import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import com.google.android.gms.ads.AdError;

import com.google.android.gms.ads.AdRequest;

import com.google.android.gms.ads.FullScreenContentCallback;

import com.google.android.gms.ads.LoadAdError;

import com.google.android.gms.ads.MobileAds;

import com.google.android.gms.ads.interstitial.InterstitialAd;

import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;

public class MainActivity extends AppCompatActivity {

private InterstitialAd mInterstitialAd;

@override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main) ;

AdRequest adRequest = new AdRequest.Builder().build();

InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,

new InterstitialAdLoadCallback() {

@override

public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {

// The mInterstitialAd reference will be null until

// an ad is loaded.

mInterstitialAd = interstitialAd;

Log.i(TAG, "onAdLoaded");

}

@override

public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {

// Handle the error

Log.i(TAG, loadAdError.getMessage());

mInterstitialAd = null;

}

});

mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){

@override

public void onAdDismissedFullScreenContent() {

// Called when fullscreen content is dismissed.

Log.d("TAG", "The ad was dismissed.");

}

@override

public void onAdFailedToShowFullScreenContent(AdError adError) {

// Called when fullscreen content failed to show.

Log.d("TAG", "The ad failed to show.");

}

@override

public void onAdShowedFullScreenContent() {

// Called when fullscreen content is shown.

// Make sure to set your reference to null so you don't

// show it a second time.

mInterstitialAd = null;

Log.d("TAG", "The ad was shown.");

}

});

if (mInterstitialAd != null) {

mInterstitialAd.show(MainActivity.this);

} else {

Log.d("TAG", "The interstitial ad wasn't ready yet.");

}

}

}