1
votes

We are using admob interstitial ads in our level based game coded in andengine. Ad is displayed properly when the game is loaded for the first time. However we want to show the ad after every level. It give gives error when the ad is called second time i.e on level over scene. We are using the following code.

"interstitial = new InterstitialAd(this, "123456789");
           adRequest = new AdRequest();
                   interstitial.loadAd(adRequest);
                   interstitial.setAdListener(ShootBalloonMainActivity.this);"

This is error: Can't create handler inside thread that has not called Looper.prepare()

We have come to conclusion that this is happening as the Main thread is not paused when the ad is shown for second time. So two threads run simultaneously which throws the error. Is that the case? If so, how do we pause the Main thread?

3
You need to dismiss the method after first time call. each an every time this method is being called not dismiss that's why this is happening.Jitendra

3 Answers

1
votes

The way i have done:

add this in your MainActivity

public void removeAd() {
        LinearLayout layoutBottom = (LinearLayout) findViewById(R.id.bottom);
        layoutBottom.removeAllViews();

    }

    private void attachAd(LinearLayout layout) {
        AdWhirlLayout adWhirlLayout = new AdWhirlLayout(this, Constants.adwhirlId);
        layout.addView(adWhirlLayout);
    }


    public void showBottomAd() {
        removeAd();
        LinearLayout layout = (LinearLayout) findViewById(R.id.bottom);
        attachAd(layout);
    }

In your every scene:

GameActivity.gameActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                GameActivity.gameActivity.showBottomAd();
            }
        }) ;
0
votes

Couple of things:

1) Separate out constructing the interstitial from loading the ad. You should only construct it once, you should load (and show) the ad after each level.

2) Can't create handler inside thread that has not called Looper.prepare(), means that the code that is generating this message needs to be called from the main UI thread (or a thread that has been configured to be such by calling Looper.prepare()). It sounds like the 2nd time you attempt to show the ad (or perhaps construct

0
votes

when you are moving from one scene to another, first you have to remove the previous instance of "adRequest" then add new instance.Place this code in "UIthread" when you calling in every scene.It will work.