![](/img/trans.png)
[英]Admob 6.4.1 - Error inflating class com.google.ads.AdView
[英]AdView Admob Error
我在该论坛上看到了许多类似的主题,但是没有一个解决了我的问题。 在我的代码中,我有这样的东西:
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 AD浏览=(的AdView)this.findViewById(R.id AD浏览 );” -adView无法解析或不是字段
-最后一行:“ adView.loadAd(adRequest) ;” -无法解析android.view.ViewGroup类型。 从所需的.class文件间接引用它
我不知道是什么原因造成的。 我之前也缺少“布局”文件夹,但是我用eclipse new> other> android XML布局文件生成了它。 我还应该以某种方式使用清单链接吗?
PS这是libGDX项目
编辑:这是我的layout / 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>
你的问题是这条线
import com.google.android.gms.R;
删除它并导入包.R文件
import <package name>.R
我在cocos2dx中遇到相同的问题,我没有布局文件,并且想散布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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.