我在这个论坛上看到过许多类似的话题,但都没有解决我的问题。在我的代码中,我有这样的东西:
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);它显示了两个错误(我将红色标记标记为粗体文本):
(AdView)this.findViewById(R.id.adView);“第一行:"AdView adView = adView -adView无法解析或不是字段
最后一行中的"adView.loadAd(adRequest);“:-And -无法解析android.view.ViewGroup类型。它是从必需的.class文件间接引用的。
我不知道是什么原因造成的。我以前也遇到过缺少“布局”文件夹的问题,但是我用eclipse布局文件生成了它。我是否也应该使用清单来链接它呢?
这是libGDX项目
编辑:下面是我的布局/Activitymain.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>
发布于 2015-06-20 12:31:42
你的问题是这条线
import com.google.android.gms.R;删除此文件并导入包.R文件
import <package name>.R发布于 2015-06-20 11:48:11
我在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);
}
}https://stackoverflow.com/questions/30953556
复制相似问题