1

我一直按照 AdMob 网站(此处:https://developers.google.com/mobile-ads-sdk/docs/?hl=en)的说明进行操作,但仍然无法让广告正常工作,但似乎没有什么问题。

这是我的布局:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:soundEffectsEnabled="true"
android:id="@+id/mainLayout"
android:background="@drawable/bg_maple"
>
[...]

<com.google.ads.AdView android:id="@+id/adView"
                     android:layout_width="fill_parent"
                     android:layout_height="wrap_content"
                     android:layout_alignParentBottom="true"
                     ads:adUnitId="my real unit id from admob"
                     ads:adSize="BANNER"
                     ads:testDevices="TEST_EMULATOR, my real device id"
                     ads:loadAdOnCreate="true"/>
[...]

在我的活动中:

public class Banner extends Activity {
      private AdView adView;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Create the adView
        adView = new AdView(this, AdSize.BANNER, "my real unit id from admob");

        // Lookup your LinearLayout assuming it's been given
        // the attribute android:id="@+id/mainLayout"
        RelativeLayout layout = (RelativeLayout)findViewById(R.id.mainLayout);

        // Add the adView to it
        layout.addView(adView);

        // Initiate a generic request to load it with an ad
        adView.loadAd(new AdRequest());

        AdRequest adRequest = new AdRequest();
        adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
        adRequest.addTestDevice("Here I have my device ID");
      }

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

感谢您提供任何帮助,我被卡住了,目前似乎无法看到问题所在。

注意:我的单位 ID 和设备 ID 都正确添加到代码中。

4

1 回答 1

1

您正在使用两个单独的 AdView,第一个来自您的 xml,第二个来自代码。请尝试更改您的代码

public class Banner extends Activity {
      private AdView adView;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Create the adView
          adView = (AdView)this.findViewById(R.id.adView);

        // Lookup your LinearLayout assuming it's been given
        // the attribute android:id="@+id/mainLayout"
       // RelativeLayout layout = (RelativeLayout)findViewById(R.id.mainLayout);

        // Add the adView to it
        //layout.addView(adView);

        // Initiate a generic request to load it with an ad
        adView.loadAd(new AdRequest());

        AdRequest adRequest = new AdRequest();
        adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
        adRequest.addTestDevice("Here I have my device ID");

      }

      @Override
      public void onDestroy() {
        if (adView != null) {
          adView.destroy();
        }
        super.onDestroy();
      }
    }
于 2012-11-14T08:18:25.507 回答