I want to show Interstitial Ads on button click but I built a separate class for Interstitial Ads and make a method in this class. Now I want that on splash screen when I enter on click button in splash screen it access the method of the Interstitial Class and show the ads but when I click on button click it give me error in Interstitial Class.
SplashScreen
package com.deitel.adsmobapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.NativeExpressAdView;
public class SplashScreen extends AppCompatActivity {
private AdView mAdview;
Button btn_move;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
btn_move=findViewById(R.id.btn_move);
btn_move.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Interstitial interstitial=new Interstitial();
interstitial.prepareAd();
Intent i=new Intent(SplashScreen.this,MainActivity.class);
startActivity(i);
}
});
/*Banner Ads*/
MobileAds.initialize(this,"ca-app-pub-3940256099942544~3347511713");
mAdview=findViewById(R.id.adView);
AdRequest adRequest=new AdRequest.Builder().build();
mAdview.loadAd(adRequest);
}
@Override
protected void onPause() {
super.onPause();
finish();
}
}
Interstitial class
package com.deitel.adsmobapp;
import android.app.Activity;
import android.app.Application;
import android.util.Log;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
public class Interstitial extends Application {
private InterstitialAd interstitialAd;
@Override
public void onCreate() {
super.onCreate();
SplashScreen splashScreen=new SplashScreen();
interstitialAd = new InterstitialAd(splashScreen);
interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequest=new AdRequest.Builder().build();
interstitialAd.loadAd(adRequest);
prepareAd();
}
public void prepareAd() {
if (interstitialAd.isLoaded())
{
interstitialAd.show();
}
else
{
Log.d("tag","Ads is not loaded");
}
}
}
Error
01-06 17:05:51.907 7044-7044/com.deitel.adsmobapp E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.deitel.adsmobapp.Interstitial.prepareAd(Interstitial.java:26)
at com.deitel.adsmobapp.SplashScreen$1.onClick(SplashScreen.java:31)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.dispatchMessage(Handler.java:92)
The error is a NPE in line 26 of your Interstiatil Class im Method prepareAd(). interstitialAd is null, because it is initialized in onCreate() which is called when the activity is created. You should change your class to inherit from Activity instead of Application. And dont call prepareAd() in your onClick Method.
public class Interstitial extends Activity
It may be smart to check for null in your prepareAd() Method:
public void prepareAd() {
if (interstitialAd != null && interstitialAd.isLoaded())
{
interstitialAd.show();
}
else
{
Log.d("tag","Ads is not loaded");
}
}