gpt4 book ai didi

java - ADMOB-java.lang.IllegalStateException : isLoaded must be called on the main UI thread

转载 作者:行者123 更新时间:2023-11-29 10:12:49 25 4
gpt4 key购买 nike

我是 android 的新手,它是我的用于中间代码的 AndroidLauncherjava 文件

       @Override public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


RelativeLayout layout = new RelativeLayout(this);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();


requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

// Create the libgdx View
View gameView = initializeForView(new MyGdxGame(this),config);


AdView adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-6916351754834612/9855033027");
adView.setAdSize(AdSize.BANNER);
adView.loadAd(new AdRequest.Builder()
.build());

layout.addView(gameView);

// Add the AdMob view
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
adView.setLayoutParams(adParams);
adView.setBackgroundColor(Color.BLACK);

layout.addView(adView, adParams);


iAd = new InterstitialAd(this);
iAd.setAdUnitId(AD_UNIT_ID);
loadInterstitial();
iAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
}

@Override
public void onAdFailedToLoad(int errorCode) {

}
});
setContentView(layout);
}

public void loadInterstitial() {
/*AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("0FD328B10106BD9B2BE832163D43D085")
.build();*/
AdRequest adRequest = new AdRequest.Builder().build();
iAd.loadAd(adRequest);

//iAd.loadAd(adRequest);
}

public void showInterstitial() {
if (iAd.isLoaded()) {
iAd.show();
} else {
//Log.d(TAG, "Interstitial ad is not loaded yet");
}
}

这是我的插页式广告代码;我收到 java.lang.IllegalStateException: isLoaded must be called on the main UI thread。在这里检查了几个解决方案,无法修复。我开始了解 iAd没有加载。任何人都可以帮助我,我哪里出错了。提前谢谢你

最佳答案

您没有显示调用 showInterstitial() 的位置,其中包含对 isLoaded 的调用!
请检查您的堆栈跟踪以获取更多线索。

我会盲目地说您正在从事件监听器或其他 (Gdx?) 后台线程调用 showInterstitial。如果是这种情况,您有两个选择:

使 showInterstitial() 万无一失

public void showInterstitial() {
if(Looper.myLooper() != Looper.getMainLooper()) {
runOnUiThread(new Runnable() {
@Override public void run() {
doShowInterstitial();
}
});
} else {
doShowInterstitial();
}
}
private void doShowInterstitial() {
if (iAd.isLoaded()) {
iAd.show();
} else {
//Log.d(TAG, "Interstitial ad is not loaded yet");
}
}

让调用代码更智能

void myMethodCallingShowInterstitial() {
... doing my other background stuff ...
// replace showInterstitial(); with below:
activityReference.runOnUiThread(new Runnable() {
@Override public void run() {
activityReference.showInterstitial();
}
});
... doing my other background stuff ...
}

在上述两种情况下,您在 runOnUiThread 之后所做的任何事情都不能依赖于显示的插页式广告!如果您不小心实现了这两个,这不是问题,因为这种简单的方法不会将它再次发布到 UI。

作为 runOnUiThread 的替代方案,您可以使用 Handler,有关更多信息,请参阅 the official documentation on this .

关于java - ADMOB-java.lang.IllegalStateException : isLoaded must be called on the main UI thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27299912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com