2014-10-01 157 views
3

我做了一個簡單的應用程序,並在其中添加了一個廣告,但它沒有出現在設備中。admob廣告不顯示

並沒有在我的代碼沒有錯誤:

XML頁面:

<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:id="@+id/RelativeLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" 
> 

     <com.google.android.gms.ads.AdView 
    xmlns:app="http://schemas.android.com/apk/libs/com.google.ads" 
    android:id="@+id/adView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="66dp" 
    ads:adSize="BANNER" 
    ads:adUnitId="ad_ip" > 
    </com.google.android.gms.ads.AdView> 
    </RelativeLayout> 

清單文件:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" ></uses-permission>  


<meta-data 
android:name="com.google.android.gms.version" 
android:value="4.3.23" /> 

<activity android:name="com.google.android.gms.ads.AdActivity"     
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" /> 
+0

你能顯示你的清單嗎? – Opiatefuchs 2014-10-01 11:51:01

+0

你爲它添加了jar文件嗎? – Namy 2014-10-01 12:01:09

+0

@ user3243147是的,我做了 – 2014-10-01 12:02:54

回答

1

我認爲這個問題是在這裏

<com.google.android.gms.ads.AdView 
    xmlns:app="http://schemas.android.com/apk/libs/com.google.ads" 
    android:id="@+id/adView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="66dp" 
    ads:adSize="BANNER" 
    ads:adUnitId="ad_ip" > 

1)xmlns屬性應該是這樣的,在我的情況下,我已經把在XML的根標籤文件。

 xmlns:ads="http://schemas.android.com/apk/res-auto" 

2)檢查時,您已經從admob帳戶中給出了實際的adUnitId。

希望這會幫助你,如果還有其他問題,你可以問。

+0

它沒有工作,我發現圖形佈局中的這個錯誤:com.google.android.gms.ads.AdView未能實例化。 – 2014-10-01 13:27:23

+0

請參閱編輯答案。我改變了第一點。我認爲現在錯誤會解決。 – Programmer 2014-10-01 14:27:16

+0

但它的代碼我試過了,仍然沒有工作,我需要在java文件中的任何代碼? – 2014-10-01 16:43:32

0

試試這個

XML:

 <com.google.ads.AdView 
     android:id="@+id/ad" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="66dp" 
     ads:adSize="BANNER" 
     ads:adUnitId="your key ID" 
     ads:loadAdOnCreate="true"/> 

添加按照您的清單代碼:

<activity 
      android:name="com.google.ads.AdActivity" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" /> 

希望這將幫助你..

+0

我收到此消息(錯誤:找不到包含 – 2014-10-01 12:16:23

+0

的屬性'loadAdOnCreate'的資源標識符,錯誤來自此屬性:ads :loadAdOnCreate =「true」 – 2014-10-01 12:18:58

+1

他使用谷歌播放服務庫,但你的例子是從老admob sdk – Opiatefuchs 2014-10-01 12:20:08

1

您正在導入你的AD瀏覽裏面的錯誤xmlns屬性(它指的是舊版AdMob):

<com.google.android.gms.ads.AdView 
xmlns:app="http://schemas.android.com/apk/libs/com.google.ads" 

您應該只使用以下指的是Google Play Admob:

xmlns:ads="http://schemas.android.com/apk/res-auto" 

==>刪除錯誤xmlns屬性

+0

我這樣做了,但廣告仍然沒有出現。 – 2014-10-01 12:32:39

+0

什麼是logcat?我的意思不只是錯誤..是否有任何來自admob的警告/信息?Admob告訴你什麼時候出現錯誤logcat – donfuxx 2014-10-01 12:34:55

+0

w這裏是你真的加載廣告?我的意思是你在你的活動中有'adView.loadAd(adRequest);'這樣的東西,是嗎? – donfuxx 2014-10-01 12:45:24

1

在我做出了同樣的錯誤開始的時候,我只用XML文件並加載didn't在我的活動視圖。所以只是把這個inisde活動:

 AdView adView = (AdView)this.findViewById(R.id.adView); 
    AdRequest adRequest = new AdRequest.Builder().build(); 
    adView.loadAd(adRequest); 
+0

我添加了代碼,但這次該應用程序在我的手機或模擬器中無法使用。 – 2014-10-01 12:58:51