2014-02-19 33 views
2

問題廣告:廣告不顯示在第一個請求,但是當它使第二請求時,它顯示的權利。在發出第二個請求前大約2秒鐘,第一個請求的廣告就會出現。有任何想法嗎? 謝謝,AdMob的Google+ Andengine只顯示在第二個請求

編輯:我改變了重力TOP,現在它顯示了第一次,但只是和廣告的上半部分,離奇的,任何想法?

@Override 
protected void onSetContentView() { 

    if(adView != null){ 
     return; 
    } 

    final FrameLayout frameLayout = new FrameLayout(this); 
    final FrameLayout.LayoutParams frameLayoutLayoutParams = 
      new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 
             FrameLayout.LayoutParams.MATCH_PARENT); 

    final FrameLayout.LayoutParams adViewLayoutParams = 
      new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 
             FrameLayout.LayoutParams.WRAP_CONTENT, 
             Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM); 

    adView = new AdView(this); 
    adView.setAdSize(AdSize.BANNER); 
    adView.setAdUnitId(getResources().getString(R.string.ad_unit_id)); 
    adView.setAdListener(new ToastAdListener(this)); 
    adView.loadAd(new AdRequest.Builder().build()); 

    this.mRenderSurfaceView = new RenderSurfaceView(this); 
    mRenderSurfaceView.setRenderer(mEngine,this); 

    final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams = 
      new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams()); 

    frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams); 
    frameLayout.addView(adView, adViewLayoutParams); 

    this.setContentView(frameLayout, frameLayoutLayoutParams); 
} 

@Override 
protected void onPause() { 
    if(adView!=null){ 
     adView.pause(); 
    } 
    super.onPause(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    if(adView!=null){ 
     adView.resume(); 
    } 
} 

@Override 
protected void onDestroy() { 
    if(adView!=null){ 
     adView.destroy(); 
    } 
    super.onDestroy(); 
} 
+0

也,當我點擊任何按鈕或後退按鈕,活動退出前,廣告正確顯示 – alexandrenofre

回答

1

我做的方法如下:

  1. 在這改變了知名度的方法,我第一次檢查是在廣告加載。如果不是,我不改變的知名度
  2. 設置AdListenerAdView。在onReceivedAd()中,我檢查條件以隱藏它 - 如果它應該隱藏,我隱藏。

正常工作這條路。

+0

我希望它是始終可見。我設置了AdListener的和活動的運行時,我看到onAdLoaded(觸發乾杯),所以它是正確加載,但它並不顯示在第一時間由於某種原因。 我不更改可見性,因爲它默認是可見的,對吧? – alexandrenofre

+0

調試我看到它始終是可見的,總是在正確的位置,並在onAdLoaded()我強迫刷新調用adView.refreshDrawableState(),仍然不能正常工作,只是後,當它再次調用請求1分: ( – alexandrenofre

0

我有同樣的問題,我解決了這個辦法:

廣告類:

public class Advertisement { 

    private AdView adView; 
    private final Handler adsHandler = new Handler(); 

    public Advertisement(final Activity activity) { 
    adView = (AdView)activity.findViewById(R.id.adView);   
    } 

    //show the ads. 
    private void showAds() { 
     adView.loadAd(new AdRequest.Builder().build()); 
     adView.setVisibility(android.view.View.VISIBLE); 
     adView.setEnabled(true); 
    } 

    //hide ads. 
    private void unshowAds() { 
     adView.loadAd(new AdRequest.Builder().build()); 
     adView.setVisibility(android.view.View.INVISIBLE); 
     adView.setEnabled(false); 
    } 

    final Runnable unshowAdsRunnable = new Runnable() { 
     public void run() { 
      unshowAds(); 
     } 
    }; 

    final Runnable showAdsRunnable = new Runnable() { 
     public void run() { 
      showAds(); 
     } 
    }; 

    public void showAdvertisement() { 
     adsHandler.post(showAdsRunnable); 
    } 

    public void hideAdvertisement() { 
     adsHandler.post(unshowAdsRunnable); 
    } 
} 

在代碼:

... 
public static Advertisement advertisement = new Advertisement(this); 
... 
public static void showAd() 
{ 
    if (advertisement != null) 
     advertisement.showAdvertisement(); 
} 
public static void hideAd() 
{ 
    if (gangsterAdvertisement != null) 
     advertisement.hideAdvertisement(); 
} 
+0

它適用於最新的谷歌播放伺服lib。 – voodoo98

+0

如何我可以使用這個類? 它顯示我編譯過程中此錯誤的所有時間 java.lang.RuntimeException:無法實例化活動ComponentInfo –

2

的問題是,通過將行解決了我 adView.setBackgroundColor(android.graphics.Color.TRANSPARENT);

相關問題