ADMOB-java.lang.IllegalStateException: isLoaded mu

2019-08-10 05:36发布

问题:

I am newbie to android,its my AndroidLauncherjava file for intersial code

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


        RelativeLayout layout = new RelativeLayout(this);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();


        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

        // Create the libgdx View
        View gameView = initializeForView(new MyGdxGame(this),config);


        AdView adView = new AdView(this);
        adView.setAdUnitId("ca-app-pub-6916351754834612/9855033027");
        adView.setAdSize(AdSize.BANNER);
        adView.loadAd(new AdRequest.Builder()
        .build()); 

        layout.addView(gameView);

        // Add the AdMob view
        RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        adParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        adView.setLayoutParams(adParams);
        adView.setBackgroundColor(Color.BLACK);

        layout.addView(adView, adParams);


       iAd = new InterstitialAd(this);
        iAd.setAdUnitId(AD_UNIT_ID);
        loadInterstitial();
        iAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                }

            @Override
            public void onAdFailedToLoad(int errorCode) {

            }
        });
        setContentView(layout);
       }

    public void loadInterstitial() {
        /*AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("0FD328B10106BD9B2BE832163D43D085")
        .build();*/
        AdRequest adRequest = new AdRequest.Builder().build();
        iAd.loadAd(adRequest);

        //iAd.loadAd(adRequest);
    }

    public void showInterstitial() {
        if (iAd.isLoaded()) {
            iAd.show();
        } else {
            //Log.d(TAG, "Interstitial ad is not loaded yet");
        }
    }

Its my code for interstitial ads;I getting java.lang.IllegalStateException: isLoaded must be called on the main UI thread .Checked few solution here,couldn't fixed .I got to know iAd is not loading.Can anyone help me, where i went wrong. Thank you in advance

回答1:

You didn't show where showInterstitial() is being called which contains the call to isLoaded!
Please check your stack trace for more clues.

Blindly I would say that you're calling showInterstitial from an event listener or other (Gdx?) background thread. If that's the case you have two options:

Make showInterstitial() fool-proof

public void showInterstitial() {
    if(Looper.myLooper() != Looper.getMainLooper()) {
        runOnUiThread(new Runnable() {
            @Override public void run() {
                doShowInterstitial();
            }
        });
    } else {
        doShowInterstitial();
    }
}
private void doShowInterstitial() {
    if (iAd.isLoaded()) {
        iAd.show();
    } else {
        //Log.d(TAG, "Interstitial ad is not loaded yet");
    }
}

Make calling code smarter

void myMethodCallingShowInterstitial() {
    ... doing my other background stuff ...
    // replace showInterstitial(); with below:
    activityReference.runOnUiThread(new Runnable() {
        @Override public void run() {
            activityReference.showInterstitial();
        }
    });
    ... doing my other background stuff ...
}

In both of the above cases anything you do after runOnUiThread CANNOT rely on interstitial being shown! If you accidentally implement both it's not a problem because the fool-proof method won't post it again to the UI.

As an alternative to runOnUiThread you can use a Handler, for more see the official documentation on this.



回答2:

Hello you can try with runOnUiThread

public void showInterstitial() {
runOnUiThread(new Runnable() {
    public void run() {
        if (iAd.isLoaded()) {
            iAd.show();
        } else {
            //Log.d(TAG, "Interstitial ad is not loaded yet");
        }
    }
});

}

Hope this help.