首页 > 解决方案 > java.lang.IllegalStateException:您不能为处于错误状态的广告调用 show()(在 Splash 之后)

问题描述

光环,

我想询问我的项目,我试图将 Audience Network 应用到我的应用程序中,正好在启动画面和这样的代码之后......

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.InterstitialAdListener;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com...Config;
import com...R;

import com.facebook.ads.*;

import static com.facebook.ads.AdSettings.IntegrationErrorMode.INTEGRATION_ERROR_CALLBACK_MODE;
import static com.facebook.ads.AdSettings.IntegrationErrorMode.INTEGRATION_ERROR_CRASH_DEBUG_MODE;

public class ActivitySplash extends AppCompatActivity {

    private InterstitialAd interstitialAd;
    private com.facebook.ads.InterstitialAd interstitialAdFB;
    private static final int RC_READ_WRITE_EXTERNAL_STORAGE_PERM = 125;
    Context context = this;
    MainActivity mActivity= new MainActivity();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash);


        FbAdLoad();

        if (Config.ENABLE_ADMOB_ON_LOAD) {

            loadInterstitialAd();
        } else {

            Log.d("AdMob", "Interstitial on load is disabled");
        }

        new CountDownTimer(3000, 1000) {
            @Override
            public void onFinish() {
                Intent intent = new Intent(getBaseContext(), MainActivity.class);
                startActivity(intent);
                finish();


                f (Config.ENABLE_ADMOB_ON_LOAD) {

                    if (interstitialAd.isLoaded()) {
                        interstitialAd.show();
                    }else{
                         interstitialAdFB.show();
                    }
                } else {
                    Log.d("AdMob", "Interstitial on load is disabled");
                }
            }

            @Override
            public void onTick(long millisUntilFinished) {

            }
        }.start();

    }

    private void FbAdLoad(){
        //com.facebook.ads.AdSettings.setIntegrationErrorMode(INTEGRATION_ERROR_CALLBACK_MODE);
        //interstitialAd = new InterstitialAd(this, "YOUR_PLACEMENT_ID");
       // interstitialAdFB = new com.facebook.ads.InterstitialAd(getApplicationContext());
        interstitialAdFB = new com.facebook.ads.InterstitialAd(context, getString(R.string.PlacementID));
        interstitialAdFB.setAdListener(new InterstitialAdListener() {
            @Override
            public void onInterstitialDisplayed(Ad ad) {
                // Interstitial ad displayed callback
              //  Log.e(TAG, "Interstitial ad displayed.");
            }

            @Override
            public void onInterstitialDismissed(Ad ad) {
                // Interstitial dismissed callback
              //  Log.e(TAG, "Interstitial ad dismissed.");
            }

            @Override
            public void onError(Ad ad, AdError adError) {
                // Ad error callback
               // Log.e(TAG, "Interstitial ad failed to load: " + adError.getErrorMessage());
            }

            @Override
            public void onAdLoaded(Ad ad) {
                // Interstitial ad is loaded and ready to be displayed
                //Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
                // Show the ad
                //interstitialAdFB.show();
            }

            @Override
            public void onAdClicked(Ad ad) {
                // Ad clicked callback
               // Log.d(TAG, "Interstitial ad clicked!");
            }

            @Override
            public void onLoggingImpression(Ad ad) {
                // Ad impression logged callback
              //  Log.d(TAG, "Interstitial ad impression logged!");
            }
        });



        // For auto play video ads, it's recommended to load the ad
        // at least 30 seconds before it is shown
        interstitialAdFB.loadAd();

    }



    private void loadInterstitialAd() {
        Log.d("TAG", "showAd");
        interstitialAd = new InterstitialAd(getApplicationContext());
        interstitialAd.setAdUnitId(getResources().getString(R.string.admob_interstitial_id));
        interstitialAd.loadAd(new AdRequest.Builder().build());
        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {

            }
        });
    }

}

运行后,我发现了这样的错误

java.lang.IllegalStateException:您不能为处于错误状态的广告调用 show()。您可以通过在 com.facebook.ads.InterstitialAd 的 com.facebook.ads.internal.cfa(Unknown Source) 的 com.facebook.ads.internal.caa(Unknown Source) 设置 AdSettings.setIntegrationErrorMode() 来更改集成错误模式。在 com.zahyakarin.BTSWallpapersKPOPNewHD.fragments.FragmentRecent.access$000(FragmentRecent.java:55) 在 com.zahyakarin.BTSWallpapersKPOPNewHD 显示(未知来源) .fragments.FragmentRecent$1$1.run(FragmentRecent.java:117) 在 android.os.Handler.handleCallback(Handler.java:754) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os。 Looper.loop(Looper.java:165) 在 android.app。

我应该怎么做才能解决这个错误?

谢谢..

标签: javascriptjavaandroidfacebook

解决方案


推荐阅读