Попытка показать рекламу, когда звук воспроизводится 10 раз, реклама не отображается и нет звука после 5-го воспроизведения

Я пытаюсь показать рекламу Когда пользователь нажимает на звуковую карту 10 раз, но когда я нажимаю на звуковую карту 10 раз, реклама не отображается и звук больше не воспроизводится, пока я не нажму кнопку остановки, после чего звук воспроизводится снова, пока я не нажму ее еще раз 10 раз Часть моей основной деятельности: ..

    MobileAds.initialize(this,
            "ca-app-pub-2470537820941001~1050614569");

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());

}
    @Override
public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mPowerSaverChangeReceiver);
        if (mSoundPlayer != null) {
            mSoundPlayer.release();
            mSoundPlayer = null;
        }
    }

@Override
public void onResume() {
    super.onResume();
    if (mSoundPlayer == null) mSoundPlayer = new SoundPlayer(MainActivity.this);
}

@Override
public void onPause() {
    super.onPause();
    if (mSoundPlayer != null) {
        mSoundPlayer.release();
        mSoundPlayer = null;
    }
}

это мой класс звукового проигрывателя:

class SoundPlayer {

private MediaPlayer mPlayer;
private Context mContext;

private static final String TAG = "SoundPlayer";
private int counter = 0;

private void playSound(Sound sound) {
    int resource = sound.getResourceId();
    if (counter == 5) {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }

    } else {
        counter++;
    }
    if (mPlayer != null) {
        if (mPlayer.isPlaying())
            mPlayer.stop();
        mPlayer.reset();
       ../

Ошибка

    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.android.gms.ads.InterstitialAd.isLoaded()' on a          null object reference
at my.app.SoundPlayer.playSound(SoundPlayer.java:38)
at my.app.reactionsounds.SoundPlayer.onEvent(SoundPlayer.java:32)
at java.lang.reflect.Method.invoke(Native Method)
at org.greenrobot.eventbus.EventBus.invokeSubscriber(EventBus.java:507)
at org.greenrobot.eventbus.EventBus.invokeSubscriber(EventBus.java:501)
at org.greenrobot.eventbus.AsyncPoster.run(AsyncPoster.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)

person ekor    schedule 05.03.2019    source источник


Ответы (1)


Вы не инициализировали межстраничное объявление в классе звукового проигрывателя. Ваша ошибка ясно показывает, что межстраничное объявление имеет значение null.

                    interstitialAd = new InterstitialAd(ReadingPage.this);
                    interstitialAd.setAdUnitId(getResources().getString(R.string.interstitial1));
                    interstitialAd.loadAd(new AdRequest.Builder().build());
person Martin Mbae    schedule 05.03.2019
comment
Это помогло? - person Martin Mbae; 06.03.2019