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!
source share