尝试在播放声音 10 次时显示广告,第 5 次播放后广告不显示且无声音



当用户按声卡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)

您尚未在声音播放器类中初始化的插入广告。

                    interstitialAd = new InterstitialAd(ReadingPage.this);
                    interstitialAd.setAdUnitId(getResources().getString(R.string.interstitial1));
                    interstitialAd.loadAd(new AdRequest.Builder().build());

相关内容

  • 没有找到相关文章

最新更新