【问题标题】:Trying to show ad when sound played 10 times, ad doesn't show & no sound after 5th time played播放 10 次声音时尝试显示广告,播放 5 次后不显示广告且没有声音
【发布时间】:2019-03-05 01:12:35
【问题描述】:

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

【问题讨论】:

    标签: java android admob soundplayer


    【解决方案1】:

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

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

    【讨论】:

    • 这有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    相关资源
    最近更新 更多