尝试播放10次声音时展示广告,播放5次之后广告不显示且没有声音

时间:2019-03-05 01:12:36

标签: java android admob soundplayer

我正在尝试显示广告当用户按声卡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)

1 个答案:

答案 0 :(得分:0)

您尚未在声音播放器类中初始化非页内广告。您的错误清楚地表明,非页内广告为空。

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