2013-03-14 71 views
4

我正在添加谷歌廣告到我的應用程序(這是我第一次)。我也添加了jar文件。添加谷歌廣告到Android應用程序

這是xml。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/headerbgcolor" 
     android:weightSum="1" > 

     <Button 
      android:id="@+id/back" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.2" 
      android:text="@string/back" 
      android:onClick="finishActivity"/> 

     <TextView 
      android:id="@+id/headertitle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_weight="0.6" 
      android:gravity="center" 
      android:text="@string/header_title" /> 

     <Button 
      android:id="@+id/home" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.2" 
      android:text="@string/home" /> 
    </LinearLayout> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="320dp" 
     android:scrollbarAlwaysDrawVerticalTrack="true" > 
    </ListView> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 


     <com.google.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     ads:adSize="BANNER" 
     ads:adUnitId="a14f26e5d930307" 
     ads:loadAdOnCreate="true" 
     ads:testDevices="AVD_for_4in_WVGA_Nexus_S,5554" /> 



    </RelativeLayout> 

</LinearLayout> 

但我無法在我的班級中定義adview,當我嘗試在班級中定義它時。

這裏是我的課,我試着去定義。

public class Cities extends Activity { 

    Button btnback = null; 
    Button btnhome = null; 
    TextView headertitle = null; 
    ListView lstcities; 
    ImageView imgfooter; 
    AdView adView; 
    public String BtnSelected; 

我得到錯誤的AdView解決不了打字,當我試圖定義

AdView adview; 

什麼poblem?

+0

檢查您的進口? – njzk2 2013-03-14 08:57:56

+0

@ njzk2我正在使用eclipse。它會自動導入所需的軟件包。但我無法導入google.ads包 – Beginner 2013-03-14 09:03:54

回答

5

我已經使用這個jar文件 - GoogleAdMobAdsSdk-4.0.4.jar

有你今年進口線

import com.google.ads.AdRequest; 
import com.google.ads.AdSize; 
import com.google.ads.AdView; 


     AdView adView = new AdView(this, AdSize.BANNER, 
        getString(R.string.publisherID)); 


     LinearLayout layout = (LinearLayout) findViewById(R.id.imgBanner); 
     layout.addView(adView); 

     AdRequest adRequest = new AdRequest(); 
     adRequest.setTesting(true); 
     // Start loading the ad in the background. 
     adView.loadAd(adRequest); 

在XML我剛纔寫這個

<LinearLayout 
    android:id="@+id/imgBanner" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" /> 

在清單文件中的代碼

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

我無法導入這些類。我在發帖之前也試過 – Beginner 2013-03-14 09:02:28

+0

你使用過哪個版本的googleads? – Nirali 2013-03-14 09:03:20

+0

我剛剛從谷歌下載。我沒有得到它是哪個版本 – Beginner 2013-03-14 09:06:38