AdMob Interstitial and error isLoaded must be called on the main UI thread

送分小仙女□ 提交于 2019-11-28 13:30:23

This is maybe not the full answer, but It´s hard to say the right answer because it´s not clear from Your code above. You´ve not showed where You use GameOver(), but I think You called it in the wrong place, I think You call it in any background thread because inside GameOver You call interstitial.isLoaded() which causing Your problem. Like the stacktrace said, call it in the main ui thread. For exqample, call this inside Your GameOver():

   runOnUiThread(new Runnable() {
        @Override public void run() {
            if (interstitial.isLoaded()) {
              interstitial.show();
         }
        }
    });

Possibly You have to call it with an activity reference, it depends from where Do You call GameOver():

    mYourActivity.runOnUiThread(new Runnable() {
        @Override public void run() {
            if (interstitial.isLoaded()) {
              interstitial.show();
         }
        }
    });

Share my case and solution.

I did new thread and call isLoaded(). It got this same error. So that I solve it by below code using Messager.

Hope this help.

Code as below.

Main thread:

public void onCreate() {

        mHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message message) {
                switch (message.what) {
                    case ACTION_ADMOB_IS_LOADED:
                        MainActivity.logForDebug(TAG,"AdMob : Check for loaded");
                        if(mIAds.isLoaded()) {
                            MainActivity.logForDebug(TAG,"AdMob : Loaded ...... OK");
                            mIAds.show();
                        }
                        break;
                }


            }
        };

}

Other thread

    public void run(){

        while(true){
                Message message;
                // ------  For AdMob ------
                Message message = mHandler.obtainMessage(ACTION_ADMOB_IS_LOADED,null);
                message.sendToTarget();
                   }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!