2014-07-14 134 views
0

AdMob廣告未在layout.My代碼顯示爲加載AdMob廣告在Android應用

layout = (LinearLayout)findViewById(R.id.LinearLayout1); 
    adView = new AdView(this); 
    adView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER); 
    adView.setAdUnitId("MY_UNIT_ID"); 
    adView.loadAd(new com.google.android.gms.ads.AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build()); 
    adView.setAdListener(new AdListener() { 
     @Override 
     public void onAdLoaded() { 
      // TODO Auto-generated method stub 
      super.onAdLoaded(); 
      layout.addView(adView,0); 
     } 
    }); 

什麼是錯的呢?但是當我從課堂上看到,AdListener時,它就顯示出來了。請幫助。謝謝

+0

@ParagChauhan他說,如果他刪除AdListener的,然後它的正常工作! –

+0

@ Parag Chauhan我從admob網站添加了正確的廣告單元ID。 「MY_UNIT_ID」就是例子 – dheerajraaj

回答

0

不要嘗試將AdView添加到onAdLoaded佈局中。只需在製作廣告時添加。即

layout = (LinearLayout)findViewById(R.id.LinearLayout1); 
adView = new AdView(this); 
adView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER); 
adView.setAdUnitId("MY_UNIT_ID"); 

layout.addView(adView,0); 

adView.loadAd(new com.google.android.gms.ads.AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build()); 
0

播放與佈局的知名度它應該工作

layout = (LinearLayout)findViewById(R.id.LinearLayout1); 
     adView = new AdView(this); 
     adView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER); 
     adView.setAdUnitId("MY_UNIT_ID"); 
     adView.loadAd(new com.google.android.gms.ads.AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build()); 
     layout.addView(adView); 
     layout.setVisibility(View.GONE); 
     adView.setAdListener(new AdListener() { 
       @Override 
       public void onAdLoaded() { 
        super.onAdLoaded(); 
        layout.setVisibility(View.VISIBLE); 
       } 
      });