Admob谷歌播放服务迁移

时间:2014-09-22 01:58:05

标签: android admob google-play-services processing

我希望能够帮助我解决一个问题,我正面临迁移到新的google play服务for 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.");
            }


    }

我已按照更新清单文件的说明来设置主用户界面,但仍然没有运气

任何帮助都会很棒!

请参阅下面的评论,我可以使用以下代码显示广告,而不是在我希望它显示时

 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);

        }



    });

2 个答案:

答案 0 :(得分:2)

通常,您可能希望将广告加载代码放在其自己的可扩展runnable的类中。但是如何从主线程中获取UI代码的快速示例:

runOnUiThread(new Runnable() 
{
    public void run() 
    {
        //ad stuff
    }
});   

答案 1 :(得分:2)

interstitial.show()上致电AdListener.onAdLoaded()。用户体验不佳,您的帐户将被取消。

您收到错误是因为您试图从man UI线程以外的线程调用interstitial.show()。

您需要将调用排到interstitial.show(),以便它在主UI线程上运行。实现这一目标的最佳方法是使用runOnUIThread(myRunnable)。 EG

private void showMyInterstitial() {
  runOnUIThread(new Runnable() {
    public void run() {
      intersitial.show();
    }
  }
}