4

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!

Harshid
  • 5,701
  • 4
  • 37
  • 50
Croc
  • 41
  • 1
  • 3

3 Answers3

3

Add the following to your project's proguard-project.txt to prevent it from stripping away the admob classes

-keep public class com.google.android.gms.ads.** {
  public *;
}

-keep public class com.google.ads.** {
  public *;
}

And also include the following activity to your manifest file

enter image description here

android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
infinity
  • 1,900
  • 4
  • 29
  • 48
0

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());
            }
        }


    }

Milan Maji
  • 363
  • 1
  • 5
  • 9
-1

You have to check that the ad is loaded before showing:

if (interstitial!=null && interstitial.isLoaded()) 
{
   interstitial.show();
}
mikek3332002
  • 3,546
  • 4
  • 37
  • 47