微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – AdMob必须在主UI线程上调用插页式广告和错误isLoaded

用户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();
         }
        }
    });

原文地址:https://www.jb51.cc/java/127471.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐