android - 加载启动画面后如何显示插页式广告?

标签 android splash-screen ads interstitial

我需要帮助,我想在初始屏幕加载后显示插页式广告,但我的代码有错误。

这是我的代码:

public class SplashScreenActivity extends AppCompatActivity {

    InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        // FIREBASE INTERSTICIAL
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-2565065222479596/3931476543");

        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                requestNewInterstitial();
            }
        });

        requestNewInterstitial();

        Toast.makeText(this,"* Necessário Acesso a Internet *",Toast.LENGTH_LONG).show();

        Thread timerThread = new Thread() {
            public void run() {
                try {
                    sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {

                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                        Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                    else
                    {
                        Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
                    }
                }

            }
        };
        timerThread.start();

    }

    // FIREBASE INTERSTICIAL
    private void requestNewInterstitial() {
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
                .build();

        mInterstitialAd.loadAd(adRequest);
    }

}

调试后得到以下信息:

01-22 16:27:03.048 13840-13970/? E/AndroidRuntime: FATAL EXCEPTION: Thread-6
                                                   Process: idea.tisco.pepavideos, PID: 13840
                                                   java.lang.IllegalStateException: isLoaded must be called on the main UI thread.
                                                       at oc.b(:com.google.android.gms.DynamiteModulesA@11951448:20)
                                                       at com.google.android.gms.ads.internal.a.d(:com.google.android.gms.DynamiteModulesA@11951448:98)
                                                       at com.google.android.gms.ads.internal.client.ak.onTransact(:com.google.android.gms.DynamiteModulesA@11951448:14)
                                                       at android.os.Binder.transact(Binder.java:499)
                                                       at com.google.android.gms.internal.zzep$zza$zza.isReady(Unknown Source)
                                                       at com.google.android.gms.internal.zzfa.isLoaded(Unknown Source)
                                                       at com.google.android.gms.ads.InterstitialAd.isLoaded(Unknown Source)
                                                       at company.ts.SplashScreenActivity$2.run(SplashScreenActivity.java:50)

即使在调试之后我也无法理解错误的原因。

我只想在启动画面之后或打开主 Activity 时显示插页式广告。

非常感谢!

最佳答案

问题就在这里java.lang.IllegalStateException: isLoaded must be called on the main UI thread.

所以可能必须这样做

 runOnUiThread(new Runnable() {
    @Override public void run() {
        if (mInterstitialAd.isLoaded()) {
          mInterstitialAd.show();
     }
    }
});

可能你必须使用 Activity 引用来调用它,这取决于你从哪里调用

 mYourActivity.runOnUiThread(new Runnable() {
    @Override public void run() {
        if (mInterstitialAd.isLoaded()) {
          mInterstitialAd.show();
     }
    }
});

来源:AdMob Interstitial and error isLoaded must be called on the main UI thread

关于android - 加载启动画面后如何显示插页式广告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48388304/

相关文章:

通知中未显示 Android 12 启动画面图标

android - 如何在 flutter app android 中添加 Startapp 广告?

android - Admob 注册/登录导致空白页面

android - 如何像Whatsapp一样在我的文档文件夹中添加文件?

android - 如何将单选按钮放在最右侧

ios - Flutter 应用程序卡在设备的初始屏幕上

ios - 启动画面 iOS 仅在模拟器上显示且尺寸错误

android - 无法获取提供商 com.google.android.gms.ads.MobileAdsInitProvider : java. lang.IllegalStateException

android - android中没有后台服务的推送通知

android - 将一个apk文件保存在另一个apk文件中