ADmob AdRequest未定义

时间:2014-05-27 13:34:20

标签: android admob

我已经实施了横幅广告并且有效! 我希望在MainActivity中实现全屏广告

代码:

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.InterstitialAd;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

public class Servizi extends FragmentActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.servizi);

      InterstitialAd mInterstitialAd = new InterstitialAd(this);
      mInterstitialAd.setAdUnitId("ca-app-pub-");

      mInterstitialAd.setAdListener(new AdListener() {
               @Override
               public void onAdLoaded() {
                   Toast.makeText(Servizi.this,
                           "The interstitial is loaded", Toast.LENGTH_SHORT).show();

               }

               @Override
               public void onAdClosed() {
                   // Proceed to the next level.
                   Toast.makeText(Servizi.this,
                           "The interstitial is CLOSED", Toast.LENGTH_SHORT).show();
               }

     });
 }

}

没有错误,但我无法看到此广告! 我的单位ID是正确的。 我不会在此广告中使用XML文件。 救命! :(

2 个答案:

答案 0 :(得分:1)

请确保您已完成http://developer.android.com/google/play-services/setup.html#Setup

编辑:

  1. Ad.java的声明在哪里?这是你的一些自定义类吗?
    
    @Override
    public void onReceiveAd(Ad ad) {
      if (ad == interstitial) {
        interstitial.show();
      }
    }
    
  2. AdRequest具有私有构造函数,可阻止您执行以下操作 AdRequest adRequest = new AdRequest();
  3. 要在InterstitialAd上设置侦听器,您想要执行以下操作
  4. 
    
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("myAdUnitId");
    
        mInterstitialAd.setAdListener(new AdListener() {
                 @Override
                 public void onAdLoaded() {
                     Toast.makeText(MyActivity.this,
                             "The interstitial is loaded", Toast.LENGTH_SHORT).show();
                 }
    
                 @Override
                 public void onAdClosed() {
                     // Proceed to the next level.
                     goToNextLevel();
                 }
             });
    

    取自官方文件http://developer.android.com/reference/com/google/android/gms/ads/InterstitialAd.html

答案 1 :(得分:0)

很简单,你是:

  1. 从不加载广告。
  2. 从不要求广告显示。
  3. 将以下内容添加到onCreate

    // Create ad request.
    final AdRequest adRequest = new AdRequest.Builder().build();
    
    // Begin loading your interstitial.
    interstitial.loadAd(adRequest);
    

    添加显示插页式广告的方法:

      // Invoke displayInterstitial() when you are ready to display an interstitial.
      public void displayInterstitial() {
        if (interstitial.isLoaded()) {
          interstitial.show();
        }
      }
    

    并在应用中的自然断点处调用displayInterstitial