当我尝试初始化广告时,我收到以下错误
AdView(Context,android.util.AttributeSet) 在AdView中无法应用 (上下文,com.google.android.gms.ads.AdSize)。
这是我的代码
public class NotesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private static final int EMPTY_VIEW = 1;
private static final int DATA_VIEW = 2;
private static final int AD_VIEW = 3;
public List<Note> allnotes;
public NotesAdapter(List<Note> allnotes, Context context) {
this.allnotes = allnotes;
this.context = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
RecyclerView.ViewHolder holder;
if (viewType == EMPTY_VIEW) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.note_zero, parent, false);
holder = new emptyViewHolder(view);
} else if (viewType ==AD_VIEW) {
view = new AdView(context, AdSize.BANNER);
} else {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.activity_note_card, parent, false);
holder = new myViewHolder(view);
}
return holder;
}
答案 0 :(得分:1)
AdSize没有AdView构造函数。使用:
AdView adView = new AdView(context);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
答案 1 :(得分:1)
下面:
新AdView(上下文,AdSize.BANNER)
您使用的是com.google.android.gms.ads.AdSize
,方法需要android.util.AttributeSet
。试试这个:
mAdView = new AdView(context);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId("myAdUnitId");
供将来参考:
显然,API已经更新,现在可用的构造函数是:
AdView(上下文上下文)
从代码构建AdView。
AdView(Context context,AttributeSet attrs)
从XML布局构造AdView。
AdView(Context context,AttributeSet attrs,int defStyle)
从XML布局构建AdView。
希望这有帮助!