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

After user William suggested that I change my code to display an interstitial ad on this

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //setDebugMode(true); initialiseAccelerometer(); interstitial = new InterstitialAd(this); interstitial.setAdUnitId(getResources().getString(R.string.InterstitialAd_unit_id)); interstitial.setAdListener(new AdListener() { public void onAdClosed() { // Create another ad request. final AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build(); interstitial.loadAd(adRequest); } }); // Create ad request. final AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build(); interstitial.loadAd(adRequest); } 

and to display an ad:

 public synchronized void GameOver() { if (lives_left > 0) { //success! - game passed - save score ScoreManager.save_localscore_simple(score_times[currentLevel], "" + currentLevel, false); if (Level_Buttons.length >= currentLevel + 1) ScoreManager.save_localscore_simple(unlocked, "unlock" + (currentLevel + 1)); if (sound_success != 0 && !sound_muted) sp.play(sound_success, 1, 1, 0, 0, 1); //open interstitial ad if (interstitial.isLoaded()) { interstitial.show(); } } else { //game not passed if (sound_gameover != 0 && !sound_muted) sp.play(sound_gameover, 1, 1, 0, 0, 1); } //open interstitial ad if (interstitial.isLoaded()) { interstitial.show(); } StopMusic(); state = GAMEOVER; } 

In every match in the game I get this error:

 W/dalvikvm(20469): threadid=11: thread exiting with uncaught exception (group=0x4180bda0) E/AndroidRuntime(20469): FATAL EXCEPTION: Thread-67543 E/AndroidRuntime(20469): Process: com.test.mygame, PID: 20469 E/AndroidRuntime(20469): java.lang.IllegalStateException: isLoaded must be called on the main UI thread. E/AndroidRuntime(20469): at com.google.android.gms.common.internal.bh.b(SourceFile:251) E/AndroidRuntime(20469): at com.google.android.gms.ads.internal.be(SourceFile:345) E/AndroidRuntime(20469): at com.google.android.gms.ads.internal.client.m.onTransact(SourceFile:66) E/AndroidRuntime(20469): at android.os.Binder.transact(Binder.java:361) E/AndroidRuntime(20469): at com.google.android.gms.internal.ap$a$a.isReady(Unknown Source) E/AndroidRuntime(20469): at com.google.android.gms.internal.au.isLoaded(Unknown Source) E/AndroidRuntime(20469): at com.google.android.gms.ads.InterstitialAd.isLoaded(Unknown Source) E/AndroidRuntime(20469): at com.test.mygame.MainGame.GameOver(MainGame.java:1128) E/AndroidRuntime(20469): at com.test.mygame.MainGame.Step(MainGame.java:773) E/AndroidRuntime(20469): at com.test.nudge.Screen.run(Screen.java:207) E/AndroidRuntime(20469): at java.lang.Thread.run(Thread.java:841) 

What is wrong here? Please explain to me as simple as possible, because I am a beginner without any programming knowledge before :) Thanks!

+6
source share
2 answers

This may not be the complete answer, but it's hard to say the right answer because it is not clear from your code above. You have not shown where you use GameOver (), but I think you called it in the wrong place, I think you are calling it in any background thread, because inside GameOver you are calling interstitial.isLoaded() , which calls your problem. As stacktrace says, name it in the ui main line. For exqample, call this inside Your GameOver ():

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

Perhaps you need to call it with a link to activity, it depends on where you call GameOver ():

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

Share your case and decision.

I made a new thread and called isLoaded (). He has the same error. So I solve it in the lower code using Messager.

I hope for this help.

The code is below.

Main theme:

 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; } } }; } 

Another topic

  public void run(){ while(true){ Message message; // ------ For AdMob ------ Message message = mHandler.obtainMessage(ACTION_ADMOB_IS_LOADED,null); message.sendToTarget(); } } 
0
source

Source: https://habr.com/ru/post/982442/


All Articles