Admob Google Play services. null pointer exception when showing interstitial ads

Viewed 3150

I'm trying to integrate admob to my game, but I'm getting nullpointerexcpetion when I'm trying to show interstitial ads. Here is my code.. Inside of onCreate


interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("a1528e2f9897fc5");

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

interstitial.loadAd(adRequest);

I'm getting error on this line.. interstitial.loadAd(adRequest);

and here is my log

12-11 17:12:41.755: E/AndroidRuntime(9357): FATAL EXCEPTION: main
12-11 17:12:41.755: E/AndroidRuntime(9357): java.lang.RuntimeException: Unable to start activity ComponentInfo{cr.logics.fastfood/cr.logics.fastfood.FastFood}: java.lang.NullPointerException
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.os.Looper.loop(Looper.java:137)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.ActivityThread.main(ActivityThread.java:4898)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at java.lang.reflect.Method.invoke(Method.java:511)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at dalvik.system.NativeStart.main(Native Method)
12-11 17:12:41.755: E/AndroidRuntime(9357): Caused by: java.lang.NullPointerException
12-11 17:12:41.755: E/AndroidRuntime(9357):     at tj.a(SourceFile:191)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at tt.onTransact(SourceFile:81)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.os.Binder.transact(Binder.java:326)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at com.google.android.gms.internal.ac$a$a.a(Unknown Source)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at com.google.android.gms.ads.InterstitialAd.loadAd(Unknown Source)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at cr.logics.fastfood.FastFood.onCreate(FastFood.java:245)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.Activity.performCreate(Activity.java:5206)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
12-11 17:12:41.755: E/AndroidRuntime(9357):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
12-11 17:12:41.755: E/AndroidRuntime(9357):     ... 11 more

I have added google-play-services-lib to my project, added meta-data into manifest, did everything as google guide, but I'm facing to this error, (sorry for my beta English). Any suggestions? Thanks in advance!

3 Answers

Before show interstitial ad you must check interstitial ad object is null or not and also check ad is loaded. Full code is given below :

In the onCreate() method of an Activity:

public class MainActivity extends Activity {

     public static InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {}
        });
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
        mInterstitialAd.setAdListener(new AdListener() {

             @Override
             public void onAdLoaded() {
                  // Code to be executed when an ad finishes loading.
             }

             @Override
             public void onAdFailedToLoad(int errorCode) {
                 // Code to be executed when an ad request fails.
             }

            @Override
            public void onAdClosed() {
                // Load the next interstitial Ad
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            }

        });
    }
}

To show the ad I used static method.So that I can call this method from another activity.

 public static void showInterstitialAd() {

        if (mInterstitialAd != null) {
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            } else {

                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            }
        }


    }

Related