2014-06-14 25 views
2

首先,我要把它放到我的清單升級到新版AdMob發佈

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

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

然後,每個佈局我希望廣告出現在裏面,我已經把這個在其.xml

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

和下來

<com.google.android.gms.ads.AdView 
    android:id="@+id/adView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    ads:adUnitId="ca-abb-psb-4557896546544844/5789887777" 
    ads:adSize="BANNER" 
    ads:loadAdOnCreate="true" 
    android:layout_gravity="center" 
    android:layout_margin="10dp"> 
</com.google.android.gms.ads.AdView>  

,最後,我已經導入庫google-play-services_libs

現在的問題是我在xml中從<com.google.android.gms.ads.AdView .... >得到error: No resource identifier found for attribute 'loadAdOnCreate' in package 'aaa.bbb.ccc'

我在所有的應用程序類中獲得R cannot be resolved to a variable

回答

9

https://developers.google.com/android/reference/com/google/android/gms/ads/AdView#xml-attributes

LoadOnCreate不能再作爲XML屬性。

<com.google.android.gms.ads.AdView 
xmlns:ads="http://schemas.android.com/apk/res-auto" 
android:id="@+id/adView" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
ads:adUnitId="MY_AD_UNIT_ID" 
ads:adSize="BANNER"/> 

// Java code required. 
// testDevices and loadAdOnCreate attributes are 
// no longer available. 
AdView adView = (AdView)this.findViewById(R.id.adView); 
AdRequest adRequest = new AdRequest.Builder() 
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
.addTestDevice("TEST_DEVICE_ID") 
.build(); 
adView.loadAd(adRequest); 
+0

非常有益地!謝謝你,朋友! –

+0

請注意,答案中的鏈接沒有用處,因爲Google正在重定向到Firebase AdMob文檔。 – dpjanes

+1

@dpjanes謝謝你指出。鏈接已被刪除。 – AlexBcn