【问题标题】:AdMob Interstitial and error isLoaded must be called on the main UI threadAdMob Interstitial 和错误 isLoaded 必须在主 UI 线程上调用
【发布时间】:2015-02-13 14:28:24
【问题描述】:

在用户 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, 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;

}

在游戏中的每次游戏结束时,我都会收到此错误:

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)

这里有什么问题?请尽可能简单地解释我,因为我是新手,之前没有任何编程知识:) 谢谢!

【问题讨论】:

    标签: java android admob interstitial


    【解决方案1】:

    这可能不是完整的答案,但很难说出正确的答案,因为上面的代码并不清楚。你没有显示你在哪里使用 GameOver(),但我认为你在错误的地方调用它,我认为你在任何后台线程中调用它,因为在 GameOver 内部你调用 interstitial.isLoaded() 导致你的问题。就像stacktrace所说的,在主ui线程中调用它。例如,在 Your 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();
             }
            }
        });
    

    【讨论】:

    • 哦,就像一个魅力! :) 你让我很开心,因为我正在寻找解决方案,以消除大约 2 周的插页式延迟!非常感谢!
    【解决方案2】:

    分享我的案例和解决方案。

    我做了新线程并调用 isLoaded()。它得到了同样的错误。 所以我使用Messager通过下面的代码来解决它。

    希望对您有所帮助。

    代码如下。

    主线程:

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

    其他话题

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-08
      相关资源
      最近更新 更多