2014-09-22 171 views
0

我希望得到一些问题,我正面临迁移到新的谷歌播放服务为admob的问题。Admob谷歌播放服务迁移

我使用了processing.org,但在Android Studio中,广告对横幅广告和插页式广告都正常工作。但是,当我尝试显示和避免时,现在出现以下错误。

adload工作正常,因为我的消息出现在我的屏幕上,但是当addcount = 300和if语句要求得到满足时,它声明isLoaded必须在主UI上调用。据我所知,我只有一个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); 

     } 



    }); 
+0

好吧,所以做了一个修正,几乎让我在那里..如果我放置interstitial.show();在setAdListner函数中,广告显示,而不是当我希望它显示时。但至少它证明服务设置正确 – user2990598 2014-09-22 09:50:18

+0

任何人都可以帮忙吗? – user2990598 2014-09-22 10:27:17

回答

2

AdListener.onAdLoaded()呼叫interstitial.show()。糟糕的用户体验,您将被禁止使用您的帐户。

由于您尝试从除man UI线程以外的Thread调用interstitial.show(),您会看到此错误。

您需要将呼叫排队到interstitial.show(),以便它在主UI线程上运行。最好的方法是使用runOnUIThread(myRunnable)。 EG

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

通常你会想要把加载广告代码,在它自己的类扩展可运行。但是,如何让UI代码从主线程快速样品:

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