当我在 UI 线程上 运行 时,为什么会出现“必须在主 UI 线程上调用 setAdListener”

Why am I getting ' setAdListener must be called on the main UI thread' when I am running it on the UI thread

我想在我的游戏中添加插页式广告。它建立在一本书的框架之上。构造函数是:

 public GameScreen(Game game) {
        super(game);
        world = new World();
        clueLetters = new String[10];
        levelNo = SettingsObject.levelUnlock;
        getWord();
        interstitialAd = new InterstitialAd(AndroidGame.context);
        interstitialAd.setAdUnitId("ca-app-pub-1861496496821617/");

        //Create an ad request
        AdRequest.Builder adRequestBuilder = new AdRequest.Builder();

        AndroidGame.activityReference.runOnUiThread(new Runnable() {
            @Override
            public void run()

            {
                // Set AdListener
                interstitialAd.setAdListener(new AdListener() {
                    @Override
                    public void onAdClosed() {
                        super.onAdClosed();
                    }
                });
            }
        });

        Looper.prepare();
        interstitialAd.loadAd(adRequestBuilder.build());

    }

但是,我收到错误消息:

 java.lang.IllegalStateException: setAdListener must be called on the main UI thread.

你能告诉我为什么会出现这个错误以及如何修复它吗?

我使用的是similar/same框架。最好的方法是在 AndroidGame activity class 中设置插页式广告,并使用界面在您的游戏屏幕中显示广告:

首先创建接口class:

public interface MyActivityListener {
    public void showInterstitial();
}

然后在你的AndroidGameclass中实现接口:

public abstract class AndroidGame extends Activity implements Game, MyActivityListener {
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        interstitialAd = new InterstitialAd(AndroidGame.context);
        interstitialAd.setAdUnitId("ca-app-pub-1861496496821617/");
        //Create an ad request
        AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
        interstitialAd.loadAd(adRequestBuilder.build());

                // Set AdListener
                interstitialAd.setAdListener(new AdListener() {
                    @Override
                    public void onAdClosed() {
                        super.onAdClosed();
                    }
                });
        }

@Override
    public void showInterstitial() {
        runOnUiThread(new Runnable() {   
            @Override
            public void run() {
                if(interstitialAd != null && interstitialAd.isLoaded())
                   interstitialAd.show();       
            }
        });

    }
}

有了这个,任何时候你想在任何 screen 中展示广告,你只需调用 showInterstitial() 方法。 您的游戏画面示例:

public class GameScreen extends Screen{
    MYActivityListener mL;
public GameScreen(Game game) {
        super(game);
        mL = (MyActivityListener)game;
        world = new World();
        clueLetters = new String[10];
        levelNo = SettingsObject.levelUnlock;
        getWord();
        mL.showInterstitial();
    }

}

我使用此实现在 UI 线程上完成所有需要 运行 的工作(隐藏和显示横幅广告、rate/share 应用程序、存储分数等。希望它有所帮助: )