我希望在迁移到admob的新谷歌播放服务时能得到一些帮助。
我使用processing.org,但在Android Studio中,横幅和间隙广告都很好。但现在我在尝试显示和避免时出现了以下错误。
adload工作正常,因为消息显示在屏幕上,但当addcount=300并且满足if语句要求时,它表示必须在主UI上调用isLoaded。据我所知,我只有一个UI,因为它只是从处理中导入的一个java文件。这是处理的限制还是有可能的解决办法?
Process: processing.test.jellycrush, PID: 24705
java.lang.IllegalStateException: isLoaded must be called on the main UI thread.
at bvz.b(SourceFile:251)
at zk.e(SourceFile:403)
at aao.onTransact(SourceFile:66)
at android.os.Binder.transact(Binder.java:361)
at com.google.android.gms.internal.aq$a$a.isReady(Unknown Source)
at com.google.android.gms.internal.av.isLoaded(Unknown Source)
at com.google.android.gms.ads.InterstitialAd.isLoaded(Unknown Source)
at processing.test.jellycrush.jellycrush.test1(jellycrush.java:738)
at processing.test.jellycrush.jellycrush.draw(jellycrush.java:328)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:841)
这是我的代码(以及相关的比特)
package processing.test.jellycrush;
//the draw section
public class jellycrush extends PApplet {
public void draw() {
addcount++;
if(addcount==300) {
test1();
}
}
//the admob code
private static final String LOG_TAG = "InterstitialSample";
//
//
private InterstitialAd interstitial;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
RelativeLayout adsLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
addcount++;
//create add
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(AD_UNIT_ID);
AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
window.addContentView(adsLayout, lp2);
//set the listener
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Log.d(LOG_TAG, "onAdLoaded");
Toast.makeText(jellycrush.this, "onAdLoaded", Toast.LENGTH_SHORT).show();
}
public void onAdFailedToLoad(int errorcode) {
}
// Only implement methods you need.
});
}
public void test1(){
if (interstitial.isLoaded()) {
interstitial.show();
} else {
Log.d(LOG_TAG, "Interstitial ad was not ready to be shown.");
}
}
我已经按照更新清单文件的说明来设置主UI,但仍然没有运气
任何帮助都会很棒!
看到下面的评论,我可以使用下面的代码来显示广告,只是当我想让它显示时不行
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Log.d(LOG_TAG, "onAdLoaded");
Toast.makeText(jellycrush.this, "onAdLoaded", Toast.LENGTH_SHORT).show();
//interstitial.show();
}
@Override
public void onAdClosed() {
AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
}
});
DoNOT在AdListener.onAdLoaded()
上调用interstitial.show()
。糟糕的用户体验,你的帐户将被禁止。
您之所以出现错误,是因为您正试图从man UI线程以外的线程调用填隙.show()。
您需要对interstitial.show()
的调用进行排队,以便它在主UI线程上运行。实现这一点的最佳方法是使用runOnUIThread(myRunnable)
。例如
private void showMyInterstitial() {
runOnUIThread(new Runnable() {
public void run() {
intersitial.show();
}
}
}
通常,您会希望将广告加载代码放在它自己的扩展可运行的类中。但是如何从主线程中获取UI代码的快速示例:
runOnUiThread(new Runnable()
{
public void run()
{
//ad stuff
}
});