Android Admob广告未在带片段的Activity中显示。为什么?

时间:2015-09-28 05:56:38

标签: android android-fragments admob android-relativelayout adview

我的活动如下。 我使用setContentView(R.layout.below);在另一个活动中启动它但是,below.xml包含一个寻呼机(它是一个片段,有自己的文本视图,webview等)。我的问题是即使在Eclipse上的图形布局中显示了Google Ads块,当我运行下面显示的java代码时也会抛出错误;我看不到广告。我不明白发生了什么!我尝试了很多东西,包括将这种布局改为简单的相对布局。以下是我称之为adView的方式。

    setContentView(R.layout.below);
    AdView mAdView2 = (AdView) findViewById(R.id.adView2);
    AdRequest adRequest2 = new     
    AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
            mAdView2.loadAd(adRequest2);

这是包含此adView的实际XML文件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" >

    <LinearLayout
        android:id="@+id/home_layout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_above="@+id/footerLayout">
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </android.support.v4.view.ViewPager>

    </LinearLayout>
    <LinearLayout
            android:id="@+id/footerLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="bottom"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            >
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0dp"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_ad_unit_id" >
    </com.google.android.gms.ads.AdView>
    </LinearLayout>


</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

尝试删除RelativeLayout的填充,也发生在我的应用程序中,但在删除填充后让它工作

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="5dp"   <--- REMOVE
    android:paddingRight="5dp" > <--- REMOVE

如果您必须使用填充,请将其添加到adview的父版面上,因此它将位于LinearLayout上

答案 1 :(得分:0)

给广告看一些高度它的高度取决于设备的屏幕尺寸,它可以是50dp,120dp

如果没有任何反应,请使用此项,请将不同的尺寸再试一次

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:id="@+id/home_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_above="@+id/footerLayout">
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>

</LinearLayout>
<LinearLayout
        android:id="@+id/footerLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"

        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        >
<com.google.android.gms.ads.AdView
    android:id="@+id/adView2"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_marginBottom="0dp"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
</LinearLayout>