用户William建议我更改我的代码以显示插页式广告
@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); }
并显示广告:
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); //open interstitial ad if (interstitial.isLoaded()) { interstitial.show(); } } else { //game not passed if (sound_gameover != 0 && !sound_muted) sp.play(sound_gameover,1); } //open interstitial ad if (interstitial.isLoaded()) { interstitial.show(); } StopMusic(); state = GAMEOVER; }
在游戏的每个游戏中我都会收到此错误:
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.b.e(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)
这有什么不对?请尽可能简单地解释我,因为我之前没有任何编程知识的新手:)谢谢!
解决方法
这可能不是完整的答案,但很难说出正确的答案,因为上面的代码并不清楚.你没有显示你在哪里使用GameOver(),但我认为你在错误的地方调用它,我认为你在任何后台线程中调用它,因为在GameOver中你调用interstitial.isLoaded()导致你的问题.就像stacktrace所说的那样,在主ui线程中调用它.例如,在您的GameOver()中调用它:
runOnUiThread(new Runnable() { @Override public void run() { if (interstitial.isLoaded()) { interstitial.show(); } } });
可能你必须用活动参考调用它,它取决于你在哪里调用GameOver():
mYourActivity.runOnUiThread(new Runnable() { @Override public void run() { if (interstitial.isLoaded()) { interstitial.show(); } } });