2014-09-22 99 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 
    } 
});