问题描述
我是android的新手,其我的AndroidLauncherjava文件用于内部代码
I am newbie to android,its my AndroidLauncherjava file for intersial code
@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必须在主UI线程上调用
。在此检查了几种解决方案,无法解决。我知道iAd没有加载。有人可以帮我吗,我哪里出错了。预先谢谢
Its my code for interstitial ads;I getting java.lang.IllegalStateException: isLoaded must be called on the main UI thread
.Checked few solution here,couldn't fixed .I got to know iAd is not loading.Can anyone help me, where i went wrong. Thank you in advance
推荐答案
您没有显示 showInterstitial()
正在被调用,其中包含对 isLoaded
的调用!
请检查堆栈跟踪以获取更多线索。
You didn't show where showInterstitial()
is being called which contains the call to isLoaded
!
Please check your stack trace for more clues.
我想说的是,您是从事件侦听器或其他(Gdx?)背景线程调用 showInterstitial
的。如果是这种情况,您有两个选择:
Blindly I would say that you're calling showInterstitial
from an event listener or other (Gdx?) background thread. If that's the case you have two options:
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");
}
}
使调用代码更智能
Make calling code smarter
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。
In both of the above cases anything you do after runOnUiThread
CANNOT rely on interstitial being shown! If you accidentally implement both it's not a problem because the fool-proof method won't post it again to the UI.
作为的替代方法runOnUiThread
可以使用 Handler
,有关更多信息,请参见。
As an alternative to runOnUiThread
you can use a Handler
, for more see the official documentation on this.
这篇关于ADMOB-java.lang.IllegalStateException:isLoaded必须在主UI线程上调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!