2015-06-20 98 views
0

我在這個論壇上看到過許多類似的主題,但他們都沒有解決我的問題。 在我的代碼,我有這樣的事情:AdView Admob錯誤

import com.google.android.gms.R; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdView; 

...

AdView adView = (AdView)this.findViewById(R.id.adView); 
AdRequest adRequest = new AdRequest.Builder() 
.addTestDevice("TEST_DEVICE_ID") 
.build(); 
adView.loadAd(adRequest); 

而且它讓我看到2個錯誤(我打上紅色標記爲粗體文本):

-In第一行:「AdView adView =(AdView)this.findViewById(R.id。adView);」 - adView無法解析或不是字段

- 在最後一行中:「adView.loadAd(adRequest);」 - 類型android.view.ViewGroup無法解析。它是從所需的.class文件間接引用的

我不知道是什麼導致它。我之前也有一個缺乏「佈局」文件夾的問題,但是我使用eclipse new> other> android XML佈局文件生成了它。我還應該以某種方式使用清單鏈接它?

P.S.這是libGDX項目

編輯: 這裏是我的佈局/ activity_main.xml中的文件:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

<com.google.android.gms.ads.AdView 
    android:id="@+id/adView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    ads:adSize="BANNER" 
    ads:adUnitId="@string/banner_ad_unit_id"> 

</com.google.android.gms.ads.AdView> 

+0

安置自己的佈局文件你在哪裏放置adview – Psypher

+0

你有沒有導入google play服務庫? –

+0

@Ravi是的,Google play服務被導入到我的項目中。 Ranjith,我會把它張貼在那裏^ – urban07

回答

1

你的問題是這一行

import com.google.android.gms.R; 

刪除此並導入包.R文件

import <package name>.R 
+0

嗯,我應該如何導入它? 當我嘗試像:「import com.name.game.R」,它不起作用。 My R.java位於「項目名稱\ android \ gen \ com \ google \ android \ gms」 – urban07

+0

如果你的packagename.R不能運行,那麼你的佈局或清單中有一些錯誤,請嘗試清理你的項目並檢查控制檯錯誤詳細信息 –

+0

哇,從來沒有,我導入Android項目到核心,它現在工作,但我現在有一個問題:「AdView adView =(AdView)this.findViewById(R.id.adView);」 - findViewById(int)方法未定義爲類型GameScreen – urban07

0

我在cocos2dx同樣的問題,我沒有佈局文件,我想dislplay AdMob的橫幅廣告,請嘗試此解決方案,而無需創建佈局的xml文件:

onCreate功能:

 adView = new AdView(this); 
     adView.setAdSize(AdSize.BANNER); 
     adView.setAdUnitId("YOUR ID"); 

     AdRequest adRequest = new AdRequest.Builder().build(); 

     adView.loadAd(adRequest); 

     adView.setBackgroundColor(Color.BLACK); 
     adView.setBackgroundColor(0); 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

     int width = getDisplaySize(getWindowManager().getDefaultDisplay()).x; 

     LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
       width, LinearLayout.LayoutParams.WRAP_CONTENT); 
     addContentView(adView, adParams); 

getDisplaySize方法:

// Helper get display screen to avoid deprecated function use 
     private Point getDisplaySize(Display d) 
      { 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
       { 
        return getDisplaySizeGE11(d); 
       } 
       return getDisplaySizeLT11(d); 
      } 

      @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) 
      private Point getDisplaySizeGE11(Display d) 
      { 
       Point p = new Point(0, 0); 
       d.getSize(p); 
       return p; 
      } 
      private Point getDisplaySizeLT11(Display d) 
      { 
       try 
       { 
        Method getWidth = Display.class.getMethod("getWidth", new Class[] {}); 
        Method getHeight = Display.class.getMethod("getHeight", new Class[] {}); 
        return new Point(((Integer) getWidth.invoke(d, (Object[]) null)).intValue(), ((Integer) getHeight.invoke(d, (Object[]) null)).intValue()); 
       } 
       catch (NoSuchMethodException e2) // None of these exceptions should ever occur. 
       { 
        return new Point(-1, -1); 
       } 
       catch (IllegalArgumentException e2) 
       { 
        return new Point(-2, -2); 
       } 
       catch (IllegalAccessException e2) 
       { 
        return new Point(-3, -3); 
       } 
       catch (InvocationTargetException e2) 
       { 
        return new Point(-4, -4); 
       } 
      } 
+0

對不起,它只是給了我更多的錯誤:/ – urban07

+0

嘗試導入類,並請將adView定義爲像這樣的全局變量\t AdView adView; –

+0

好吧,有多個錯誤。給我一分鐘,我會盡力將它們全部粘貼在這裏 – urban07