【发布时间】:2014-09-22 01:58:04
【问题描述】:
我希望在迁移到适用于 admob 的新 google play 服务时遇到的问题获得一些帮助。
我使用了 processing.org,但在 Android Studio 中,广告在横幅广告和插页式广告中都运行良好。但是现在当我尝试显示和避免时出现以下错误。
加载工作正常,因为我的屏幕上出现了消息,但是当 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);
}
});
【问题讨论】:
-
好的,所以做了一个修改,几乎让我到了那里..如果我放置 interstitial.show();在 setAdListner 函数中,广告会显示,而不是在我希望它显示的时候。但至少它证明服务设置正确
标签: android admob google-play-services processing