AdMob插页和错误isLoaded必须调用主UI线程上 [英] AdMob Interstitial and error isLoaded must be called on the main UI thread

查看:2803
本文介绍了AdMob插页和错误isLoaded必须调用主UI线程上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户威廉建议我改变我的code为显示插播式广告这种

After user William suggest me to change my code for showing interstitial ad to 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);
}

和用于显示广告的:

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;

}

在游戏中的每一个GAMEOVER我得到这个错误:

On every gameover in 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.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)

什么是错在这里?请给我解释一下简单越好,因为我真的新手没有任何之前:)编程知识的感谢!

What is wrong here? Please explain me simple as possible because I`m newbie without any programming knowledge before :) Thanks!

推荐答案

这是也许不是完整的答案,但很难汇入作业说出了正确答案,因为没有汇入作业从上面你code清楚。 ......你不显示在您使用GAMEOVER(),但我认为你在错误的地方叫吧,我想是因为里面GAMEOVER你叫你把它叫做任何后台线程 interstitial.isLoaded()这造成您的问题。像堆栈跟踪说,把它在主UI线程。对于exqample,称这个你GAMEOVER()内:

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

也许你有一个活动的参考称呼它,这取决于您从哪里调用GAMEOVER():

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

这篇关于AdMob插页和错误isLoaded必须调用主UI线程上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆