AdMob 横幅问题 android

AdMob banner issues android

我想像这样在 FrameLayout 下面的 MainActivity 中放置广告:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
xmlns:ads="http://schemas.android.com/apk/res-auto">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:id="@+id/fragmentContainer"
    android:layout_centerHorizontal="true">
</FrameLayout>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>

我已将此添加到我的清单中: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>

我的 build.gradle 看起来也不错。这是我的 java 代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MobileAds.initialize(getApplicationContext(),"ca-app-pub-xxxxxxxx~xxx");
    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest request = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)  // An example device ID
            .build();
    mAdView.loadAd(request);

但是当我 运行 应用程序时我看不到广告并且在 LogCat 我得到了这个错误:

W/Ads: Invalid unknown request error: [Cannot determine request type. Is your ad unit id correct?]

W/Ads: There was a problem getting an ad response. ErrorCode: 1

W/Ads: Failed to load ad: 1

我几乎尝试了在网上找到的所有方法,但没有任何效果。我认为问题可能出在我的 activity_main.xml 中,因为 FrameLayout 没有为广告提供足够的位置。

您的代码看起来不错(尽管您应该从 Application class 的 onCreate() 方法中调用 MobileAds.initialize(),而不是从 Activity 中调用。

看起来唯一的问题可能是 banner_ad_unit_id 资源是无效的广告单元 ID(错误消息也提示)。