0

Hello I want to run an InterstitialAd Every time the method called play(RadioListElement rle) execute in my app but it gives me this error

Class MusicPlayer

public class MusicPlayer extends Context {
private static MediaPlayer mediaPlayer;
private InterstitialAd InterstitialAd;
private AdRequest adsRequest;

public void play(RadioListElement rle) {
    radioListElement = rle;
    playMusic(radioListElement.getUrl());
    adsRequest = new AdRequest.Builder().build();
    /*Prepare the Interstitial Ad*/
    InterstitialAd = new InterstitialAd(this);
    /* Insert the Ad Unit ID */
    InterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    InterstitialAd.loadAd(adsRequest);
    /* Prepare an Interstitial Ad Listener*/
    InterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded () {                
  displayInterstitial();         }        });    }

private void displayInterstitial() {
if (InterstitialAd.isLoaded()) {
InterstitialAd.show();     }    }

LOG

26421-26421/com.radio.stations E/GooglePlayServicesUtil: The Google Play services resources were not found. Check your project configuration to ensure that the resources are included. 07-18 16:52:59.049 26421-26421/com.radio.stations W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageInfo android.content.pm.PackageManager.getPackageInfo(java.lang.String, int)' on a null object reference 07-18 16:52:59.049 26421-26421/com.radio.stations W/System.err: at com.google.android.gms.common.zze.isGooglePlayServicesAvailable(Unknown Source) 07-18 16:52:59.049 26421-26421/com.radio.stations W/System.err: at com.google.android.gms.common.zzc.isGooglePlayServicesAvailable(Unknown Source) 07-18 16:52:59.049 26421-26421/com.radio.stations W/System.err: at com.google.android.gms.ads.internal.util.client.zza.zzar(Unknown Source) 07-18 16:52:59.049 26421-26421/com.radio.stations W/System.err: at com.google.android.gms.ads.internal.client.zzl.zza(Unknown Source) 07-18 16:52:59.049 26421-26421/com.radio.stations W/System.err: at com.google.android.gms.ads.internal.client.zzl.zzb(Unknown Source) 07-18 16:52:59.049 26421-26421/com.radio.stations W/System.err: at com.google.android.gms.ads.internal.client.zzaf.zzam(Unknown Source) 07-18 16:52:59.049 26421-26421/com.radio.stations W/System.err: at com.google.android.gms.ads.internal.client.zzaf.zza(Unknown Source) 07-18 16:52:59.049 26421-26421/com.radio.stations W/System.err: at com.google.android.gms.ads.InterstitialAd.loadAd(Unknown Source) 07-18 16:52:59.049 26421-26421/com.radio.stations W/System.err: at com.Radio.Stations.MusicPlayer.play(MusicPlayer.java:125) 07-18 16:52:59.049 26421-26421/com.radio.stations W/System.err: at com.Radio.Stations.RadioList.nextOrPreviousRadioStation(RadioList.java:82) 07-18 16:52:59.049 26421-26421/com.radio.stations W/System.err: at com.Radio.Stations.MusicPlayerControl$2.onTouch(MusicPlayerControl.java:94) 07-18 16:52:59.049 26421-26421/com.radio.stations W/System.err: at android.view.View.dispatchTouchEvent(View.java:8802)

build.gradle project com.radio.stations

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "com.Switzerland.radio.stations"
    multiDexEnabled true
    minSdkVersion 14
    targetSdkVersion 23

}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'        }    }}
dependencies {
compile project(':initActivity')
compile 'com.android.support:support-v4:24.0.0'
compile 'com.google.android.gms:play-services:9.2.0'}
Liam
  • 27,717
  • 28
  • 128
  • 190
Mohaid Y
  • 11
  • 1
  • 2

1 Answers1

1

you need to pass the context of either of your activity (as this) or your application . you can try this

public class MusicPlayer extends ActionBarActivity 

and there are couple of error as well which starts from following line

InterstitialAd = new InterstitialAd(this);

where is the reference variable of InterstitialAd. try google guideline for ads, for better understanding and clear examples.

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • Thanks @Pavneet Singh i did what you said but now gives me this error at com.google.android.gms.ads.InterstitialAd.loadAd(Unknown Source)com.Switzerland.radio.stations W/System.err: at com.Switzerland.Radio.Stations.MusicPlayer.play(MusicPlayer.java:129) – Mohaid Y Jul 18 '16 at 18:27
  • you previous code wan't following the right flow and i don't know what you have changed and where u have. whole code for google adds are available online with explanation. – Pavneet_Singh Jul 19 '16 at 09:41