android - 无法在我的 Android 应用中集成 AdMob 广告

标签 android admob

我一直按照 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"/>
[...]

在我的 Activity 中:

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 都已正确添加到代码中。

最佳答案

您正在使用两个单独的 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();
      }
    }

关于android - 无法在我的 Android 应用中集成 AdMob 广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13374924/

相关文章:

android - 如何构建安卓框架

将 admob 原生广告添加到回收站 View 后,Android 获得正确的项目位置

java - 将 Admob 添加到 Android 应用程序 : AdView

android - admob 中自定义插页式广告的图像尺寸

Android 添加 AdView 到 HTML5WebView

java - 插页式测试广告在 Android Studio 中自行关闭。这是正常的吗?

android.security.KeyStoreException -62

android - 如何获取android中加载mapview的进度状态?

android - ADB is not working and failed to initialize monitor thread 错误发生在android中的ddms

Android获取所有ListView项目的截图