2013-11-14 83 views
1

你好,我已經做了應用程序。但我的admob廣告與飛濺activitvity工作正常。但不會顯示在我的activity_main上。它顯示的很好,但它好像沒有剩餘空間,因爲RelativeLayout顯示廣告。admob沒有顯示,因爲「也許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); 

回答

0

這裏有幾個問題。

1)您正在使用XML定義AdView,但隨後嘗試將其第二次添加到您的RelativeLayout代碼中。注意:由於RelativeLayout爲null,因此代碼添加將失敗並返回NullPointerException。只需在XML中定義您的AdView。

2)用XML定義RelativeLayout的位置指定了一個無效的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,但你也通過加載代碼的廣告。選一個。