我是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没有加载。任何人都可以帮助我,我出错了。提前谢谢
答案 0 :(得分:4)
您好,您可以尝试使用runOnUiThread
public void showInterstitial() {
runOnUiThread(new Runnable() {
public void run() {
if (iAd.isLoaded()) {
iAd.show();
} else {
//Log.d(TAG, "Interstitial ad is not loaded yet");
}
}
});
}
希望得到这个帮助。
答案 1 :(得分:4)
您没有显示调用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
之后您所做的任何事情都不能依赖于显示插页式广告!如果你不小心实现了这两个问题,因为这种傻瓜式方法不会再次发布到用户界面。
作为runOnUiThread
的替代方案,您可以使用Handler
,有关详情,请参阅the official documentation on this。