【发布时间】:2018-01-22 18:44:50
【问题描述】:
我需要帮助,我想在初始屏幕加载后显示一个插页式添加,但我的代码有错误。
这是我的代码:
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)
即使在调试之后我也无法理解错误的原因。
我只想在初始屏幕之后或打开 mainactivity 时显示一个插页式广告。
非常感谢!
【问题讨论】:
-
您希望在启动画面结束后显示广告,那么您应该在我的意思是在您的 mainActivity 的 ON CREATE 中使用插页式广告加载和显示过程
标签: android splash-screen ads interstitial