无法让AdMobs在地图上显示

时间:2014-04-10 10:34:21

标签: android xml google-maps admob

我已按照本教程:AdMob Code

但是我无法显示横幅(甚至不是我在网上看过很多常见的XML adSize错误:

这是我的main_activity中的代码:

private AdView adView;
    private static final String AD_UNIT_ID = "ca-app-pub-XXXXXXXXXXXXXXXXXX";
    GoogleMap mMap;
    LatLng myposition;
    Marker marker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Create an ad.
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);

        //Add the AdView to the view hierarchy.The view will have no size
        //Until the ad is loaded.
        LinearLayout layout = (LinearLayout) findViewById(R.id.lyout);
        layout.addView(adView);

        //Create an ad request. Check kogcat output for the hashed device ID to
        //get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("MY_DEVICE_ID")
        .build();

        //Start loading the ad in the background.
        adView.loadAd(adRequest);

}

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/lyout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
     >


<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
    android:name="com.google.android.gms.maps.MapFragment"
    android:id="@+id/the_map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    map:cameraTilt="45"
    map:cameraZoom="15"
    />
</LinearLayout>

起初我的布局文件只有:

 <fragment xmlns:map="http://schemas.android.com/apk/res-auto"
        android:name="com.google.android.gms.maps.MapFragment"
        android:id="@+id/the_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        map:cameraTilt="45"
        map:cameraZoom="15"
        />

编辑:将LogCat删除为正常工作

但那也没有用。如何展示这些广告?

由于

1 个答案:

答案 0 :(得分:0)

您可以从xml布局文件中加载如下所示的横幅广告:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ads="http://schemas.android.com/apk/res-auto"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    <com.google.android.gms.ads.AdView android:id="@+id/adView"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             ads:adUnitId="MY_AD_UNIT_ID"
             ads:testDevices="TEST_EMULATOR"
             ads:loadAdOnCreate="true"
             ads:adSize="BANNER"/>
    <fragment xmlns:map="http://schemas.android.com/apk/res-auto"
             android:name="com.google.android.gms.maps.MapFragment"
             android:id="@+id/the_map"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             map:cameraTilt="45"
             map:cameraZoom="15" />
</LinearLayout>

注意:如果您最近在admob帐户中生成了appId,那么可能没有广告要显示您的Admob引擎ID,请等待一段时间然后再试一次。也许这也会奏效。

希望它会帮助你..