2015-06-20 101 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