我需要帮助,我想在启动屏幕加载后显示间隙添加,但我的代码有错误。
这是我的代码:

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)

即使在调试之后,我也无法理解错误的原因。
我只想在启动屏幕后或打开主活动时显示一个间隙。
非常感谢!

最佳答案

问题就在这里java.lang.IllegalStateException: isLoaded must be called on the main UI thread.
所以可能必须这么做

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

可能需要用活动引用调用它,这取决于从何处调用
 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/

10-11 06:25
查看更多