当前位置: 首页 > 面试题库 >

必须在主UI线程上调用AdMob非页内广告和错误isLoaded

唐高朗
2023-03-14
问题内容

用户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.html" target="_blank">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();
         }
        }
    });


 类似资料:
  • 问题内容: 方法getText()必须从UI线程调用,请提供帮助。我是android studio的初学者,可以在网上找到这些代码,但无法弄清楚,我真的很感激。 问题答案: 在其UI线程而非后台线程中获取值。

  • 问题内容: 我的主要xml文件中有这个: 我已经设置了广告尺寸和单位ID,但是在运行时(来自MainActivity.java), 标题中有一个例外。 问题答案: 我在github示例中找到了解决方案,即: 代替 删除xmlns:ads ***标记并添加 标记为LinearLayout标记,如下所示: 而已 :) 这个xml的github链接

  • 问题内容: 我正在尝试为应用程序创建登录名。但是我有一个问题。 这是我的代码: Android studio表示必须在istructions的UI线程中调用该方法:以及 可能的解决方案? 问题答案: 尝试将您的值传递给 via 方法:

  • 我已经在我的应用程序中开发了本机adMob,方法是在下面的URL中处理来自google提供的示例应用程序的代码https://github.com/googleads/googleads-mobile-ios-examples/tree/master/Objective-C/admob/NativeExample 我使用我的AdUnitID得到:Error:失败,错误为:请求错误:没有要显示的广告

  • 我已经编写了代码,用于根据 https://developers.google.com/mobile-ads-sdk/docs/admob/advanced 将 admob 中的插页式广告添加到应用的菜单活动中。我也在清单中添加了权限和配置更改。我在下面提到的行中遇到错误: ` '

  • 我试图从WebView中javascript调用的方法调用WebView方法。然后Webview应该返回方法中使用的值。 html事件- 显然,Webview在ui线程中运行,而js不运行,webview的方法必须从同一个线程调用。此方法可用于从非ui线程调用方法: 但我也想返回结果<代码>返回webView。例如,getUrl()。我该怎么做?