Search code examples
androidin-app-purchaseadmobin-app-billinginterstitial

How to correctly Remove Ads In-App Purchase


I successfully managed to implement the Google Billing Library in my project. I've added an in-app purchase to remove ads in the application, I tested it and the ads are removed after purchase, but when the interstitial ad should appear (it should not appear if I purchased the item), I get app crash.

I noticed that this happens for the regular users too that haven't paid for remove ads.

Can this happen because the application is saved as a draft on Google Play and it is not live? I really don't want to publish it with this possible bug, because people may find it annoying and they may uninstall the app forever.

Please let me know if this is the cause, on the other case, I will edit my post and paste some code in there.

Here is something from LogCat:

05-05 22:23:52.660: E/AndroidRuntime(16730): FATAL EXCEPTION: main
05-05 22:23:52.660: E/AndroidRuntime(16730): Process: com.my.app, PID: 16730
05-05 22:23:52.660: E/AndroidRuntime(16730): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.android.gms.ads.InterstitialAd.isLoaded()' on a null object reference
05-05 22:23:52.660: E/AndroidRuntime(16730):    at com.my.app.GameActivity$4.run(GameActivity.java:211)
05-05 22:23:52.660: E/AndroidRuntime(16730):    at android.os.Handler.handleCallback(Handler.java:739)
05-05 22:23:52.660: E/AndroidRuntime(16730):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-05 22:23:52.660: E/AndroidRuntime(16730):    at android.os.Looper.loop(Looper.java:145)
05-05 22:23:52.660: E/AndroidRuntime(16730):    at android.app.ActivityThread.main(ActivityThread.java:5834)
05-05 22:23:52.660: E/AndroidRuntime(16730):    at java.lang.reflect.Method.invoke(Native Method)
05-05 22:23:52.660: E/AndroidRuntime(16730):    at java.lang.reflect.Method.invoke(Method.java:372)
05-05 22:23:52.660: E/AndroidRuntime(16730):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
05-05 22:23:52.660: E/AndroidRuntime(16730):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

I'm using a boolean called "NOADS" to check if the user purchased the item:

private void itemPurchased() {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        Editor editor = preferences.edit();
        editor.putBoolean("NOADS", true);
        editor.commit();
}

I show or hide the ads for users who purchased with the following:

public void addInMainActivity() {
        if (!PreferenceManager.getDefaultSharedPreferences(this).getBoolean("NOADS", false)) {

            AdView ad = (AdView) findViewById(R.id.adView);
            ad.loadAd(new AdRequest.Builder().build());

            // Create the interstitial.
            interstitial = new InterstitialAd(this);
            interstitial.setAdUnitId(getResources().getString(R.string.adInterstitialUnitId));

            // Create ad request.
            AdRequest adRequest = new AdRequest.Builder().build();

            // Begin loading your interstitial
            interstitial.loadAd(adRequest);
            } else {
                ((AdView) findViewById(R.id.adView)).setVisibility(View.GONE);
            }
}

Thanks for the help!


Solution

  • Look at line #211 of GameActivity. You are calling interstitial.isAdLoaded(). At this point in time interstitial is null.