admob没有显示因为“也许RelativeLayout”

时间:2013-11-14 19:49:50

标签: android admob

您好我已经制作了该应用。但我的admob广告在飞溅活动中运作良好。但不会在我的activity_main上显示。它看起来很好,但似乎没有空间因为RelativeLayout而显示广告。

什么是错的,请帮助。

这是XML代码。

<?xml version="1.0" encoding="utf-8"?>
<!-- suppress ALL -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/item_select"
android:orientation="horizontal" >

<!-- ListRow Left sied Thumbnail image -->

<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dip"
    android:background="#fbf4ff"
    android:padding="10dip" >

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:contentDescription="@string/imageViewDescription"
        android:src="@drawable/icon_24" />

</LinearLayout>
<!-- Title Of Song -->

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="@string/RihannaLove"
    android:textColor="#ffffff"
    android:textSize="14sp"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/rating"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:src="@drawable/icon_rating" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_above="@layout/activity_main"
    android:layout_alignParentLeft="true"
    android:gravity="top"
    android:ignoreGravity="@drawable/background_item"
    android:visibility="visible" >

    <com.google.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads"
        android:id="@+id/ad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:loadAdOnCreate="true"
        android:gravity="bottom"
        android:ignoreGravity="@drawable/item_select"
        app:adSize="SMART_BANNER"
        app:adUnitId="ca-app-pub-2235803990161195/8451367464" >
    </com.google.ads.AdView>

</RelativeLayout>

</RelativeLayout>

**and here is  code were admob is on** 

private void setRingstone(int id, String path, String RingstoneName, String mode) 
        throws NotFoundException, IllegalArgumentException, IOException,         IllegalAccessException {

    //Move ringtone to sdcard
    moveRingtoneToSDcard(id, Environment.getExternalStorageDirectory() + "/" +         folder_name_in_sdcard + "/" + path + ".mp3");
    // Music file will choose to set new Ringtone
    String filepath = Environment.getExternalStorageDirectory() + "/" + folder_name_in_sdcard + "/" + path + ".mp3";
    Log.i("TAG", filepath);
    File ringtoneFile = new File(filepath);

    ContentValues content = new ContentValues();
    content.put(MediaStore.MediaColumns.DATA, ringtoneFile.getAbsolutePath());
    content.put(MediaStore.MediaColumns.TITLE, RingstoneName);
    content.put(MediaStore.MediaColumns.SIZE, 215454);
    content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
    content.put(MediaStore.Audio.Media.ARTIST, "");
    content.put(MediaStore.Audio.Media.DURATION, 230);
    content.put(MediaStore.Audio.Media.IS_MUSIC, false);

    // Set as Ringtone
    if (mode.equals(Ringtone))
        content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    else
        content.put(MediaStore.Audio.Media.IS_RINGTONE, false);

    // Set as Notification
    if (mode.equals(Notification))
        content.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
    else
        content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);

    // Set as Alarm
    if (mode.equals(Alarm))
        content.put(MediaStore.Audio.Media.IS_ALARM, true);
    else
        content.put(MediaStore.Audio.Media.IS_ALARM, false);
    adView =  (AdView)findViewById(R.id.ad);
    adView.setTag("adView");
    adView.refreshDrawableState();
    adView.setVisibility(AdView.VISIBLE);
    adView = (AdView)findViewById(R.id.ad);
    AdView.LayoutParams adViewParams = new AdView.LayoutParams(
        AdView.LayoutParams.WRAP_CONTENT, 
        AdView.LayoutParams.WRAP_CONTENT);
    //the next line is the key to putting it on the bottom
    adViewParams.addRule(AdView.ALIGN_PARENT_BOTTOM);

    ViewGroup relativeLayout = null;
    relativeLayout.addView(adView, adViewParams);

1 个答案:

答案 0 :(得分:0)

这里有几个问题。

1)您在XML中定义AdView,然后尝试在代码中再次将其添加到RelativeLayout。注意:由于RelativeLayout为null,代码添加将失败并显示NullPointerException。只需在XML中定义AdView即可。

2)RelativeLayout在XML中的位置定义指定了无效的layout_above。您需要引用当前布局中存在的布局元素。如果您希望广告显示在缩略图上方,我建议您使用以下内容:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_above="@id/thumbnail"
    android:layout_alignParentLeft="true"
    android:gravity="top"
    android:ignoreGravity="@drawable/background_item"
    android:visibility="visible" >

    <com.google.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads"
        android:id="@+id/ad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:loadAdOnCreate="true"
        android:gravity="bottom"
        android:ignoreGravity="@drawable/item_select"
        app:adSize="SMART_BANNER"
        app:adUnitId="ca-app-pub-2235803990161195/8451367464" >
    </com.google.ads.AdView>

</RelativeLayout>

3)您已在XML中指定了loadOnCreate,但您也是通过代码加载广告。选择一个。