1

您好,我已经制作了该应用程序。但是我的 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);
4

1 回答 1

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,但您也在通过代码加载广告。选一个。

于 2013-11-15T23:06:19.093 回答