插页式广告Admob,illegalStateException

时间:2018-04-05 08:30:19

标签: android admob ads interstitial banner-ads

我实现了一个在我的应用中显示插页式广告的结构,我在Application类中声明了插页式广告对象,因此可以跨活动访问它,不幸的是我在我设置广告单元ID的行上获取了illegalStateExceptioninterstitialAd.setAdUnitId(addunitId)。可能的原因可能是什么。

这是我的代码

MyCustomApplication

    public class MyCustomApplication extends Application {

    public InterstitialAd interstitialAd=null;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public void initialAdsSetUp() {

        if (interstitialAd == null)
            interstitialAd = new InterstitialAd(this);

        interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                startLoadingAds();
            }

        });
        startLoadingAds();
    }

    //--------------------------------------------------------
    void startLoadingAds() {
        if (!interstitialAd.isLoading() && !interstitialAd.isLoaded()) {
            AdRequest adRequest = new AdRequest.Builder().build();
            interstitialAd.loadAd(adRequest);
        }
    }

    //---------------------------------------------------------------
    void showAd() {
        if (interstitialAd != null)
            if (interstitialAd.isLoaded()) {
                interstitialAd.show();
            }
    }
    //-----------------------------------

}

启动画面

public class SplashScreen extends Activity {

private static int SPLASH_TIME_OUT = 3000;
private MyCustomApplication admobApp;

//----------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    admobApp = (MyCustomApplication) getApplication();
    admobApp.initialAdsSetUp();

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(i);
            finish();
        }
    }, SPLASH_TIME_OUT);

} //onCreate() closed

} 

0 个答案:

没有答案
相关问题