将 admob 原生广告加载到 recyclerview 时出现问题
Problem loading admob native ads into recyclerview
我正在尝试在 recyclerview 中加载 admob 原生广告。我使用了原生广告模板。我已经在主 activity(onCreate).
中初始化了广告
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
这是我的items_ads.xml
<com.google.android.ads.nativetemplates.TemplateView android:id="@+id/my_template"
app:gnt_template_type="@layout/gnt_small_template_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" />
这是我的适配器class
public class WallpaperzAdapter 扩展 RecyclerView.Adapter {
private final int CONTENT_TYPE=1;
private final int AD_TYPE=2;
private Activity _activity;
private List<Photo> wallpapersList = new ArrayList<Photo>();
Context context;
public WallpaperzAdapter(Activity activity, List<Photo> wallpapersList) {
this._activity = activity;
this.wallpapersList = wallpapersList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == AD_TYPE) {
adViewHolder madViewHolder = new adViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ads, null, false));
return madViewHolder;
} else{
WallpaperViewHolder mYourViewHolder = new WallpaperViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_wallpaperz, null, false));
return mYourViewHolder;
}
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
if (getItemViewType(position) == CONTENT_TYPE) {
final Photo p = wallpapersList.get(position);
((WallpaperViewHolder) holder).thumbNail.setVisibility(View.VISIBLE);
Glide.with(_activity)
.load(p.getSrc().getPortrait())
.thumbnail(0.5f)
.into(((WallpaperViewHolder) holder).thumbNail);
((WallpaperViewHolder) holder).thumbNail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("","");
}
});
} else if (getItemViewType(position) == AD_TYPE){
final AdLoader adLoader = new AdLoader.Builder(context, "ca-app-pub-3940256099942544/2247696110")
.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
// Show the ad.
NativeTemplateStyle styles = new
NativeTemplateStyle.Builder().build();
TemplateView template = ((adViewHolder) holder).Adtemplate;
template.setStyles(styles);
template.setNativeAd(unifiedNativeAd);
}
})
.withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) {
// Handle the failure by logging, altering the UI, and so on.
}
})
.withNativeAdOptions(new NativeAdOptions.Builder()
// Methods in the NativeAdOptions.Builder class can be
// used here to specify individual options settings.
.build())
.build();
adLoader.loadAd(new AdRequest.Builder().build());
}}
@Override
public int getItemCount() {
return wallpapersList.size();
}
public class WallpaperViewHolder extends RecyclerView.ViewHolder {
public ImageView thumbNail;
final ProgressBar imageloader;
public WallpaperViewHolder(View itemView) {
super(itemView);
// Grid thumbnail image view
imageloader = itemView.findViewById(R.id.imgLoader);
thumbNail = itemView
.findViewById(R.id.home_image_item_view);
}
}
class adViewHolder extends RecyclerView.ViewHolder {
TemplateView Adtemplate;
public adViewHolder(@NonNull View itemView) {
super(itemView);
Adtemplate = itemView.findViewById(R.id.my_template);
}
}
@Override
public int getItemViewType(int position) {
if ((position+1) % 5 == 0 && (position+1) != 1) {
return AD_TYPE;
}
return CONTENT_TYPE;
}
}
当我启动应用程序时,出现以下错误,该错误位于我尝试为 AD_TYPE
加载广告的 onBindViewHolder 中
java.lang.NullPointerException: context cannot be null
at com.google.android.gms.common.internal.Preconditions.checkNotNull(Unknown Source:11)
at com.google.android.gms.ads.AdLoader$Builder.<init>(com.google.android.gms:play-services-ads-lite@@19.3.0:2)
at com.amatyasajal.hdwallpaperz.adapter.WallpaperzAdapter.onBindViewHolder(WallpaperzAdapter.java:104)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
请帮助我,这是我第一次使用 admob,我很困惑。提前致谢。
嘿,从你的代码来看,上下文确实是空的,你必须先在构造函数中初始化它,或者只使用 _activity 它会起作用,让我知道
我正在尝试在 recyclerview 中加载 admob 原生广告。我使用了原生广告模板。我已经在主 activity(onCreate).
中初始化了广告MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
这是我的items_ads.xml
<com.google.android.ads.nativetemplates.TemplateView android:id="@+id/my_template"
app:gnt_template_type="@layout/gnt_small_template_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" />
这是我的适配器class
public class WallpaperzAdapter 扩展 RecyclerView.Adapter
private final int CONTENT_TYPE=1;
private final int AD_TYPE=2;
private Activity _activity;
private List<Photo> wallpapersList = new ArrayList<Photo>();
Context context;
public WallpaperzAdapter(Activity activity, List<Photo> wallpapersList) {
this._activity = activity;
this.wallpapersList = wallpapersList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == AD_TYPE) {
adViewHolder madViewHolder = new adViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ads, null, false));
return madViewHolder;
} else{
WallpaperViewHolder mYourViewHolder = new WallpaperViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_wallpaperz, null, false));
return mYourViewHolder;
}
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
if (getItemViewType(position) == CONTENT_TYPE) {
final Photo p = wallpapersList.get(position);
((WallpaperViewHolder) holder).thumbNail.setVisibility(View.VISIBLE);
Glide.with(_activity)
.load(p.getSrc().getPortrait())
.thumbnail(0.5f)
.into(((WallpaperViewHolder) holder).thumbNail);
((WallpaperViewHolder) holder).thumbNail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("","");
}
});
} else if (getItemViewType(position) == AD_TYPE){
final AdLoader adLoader = new AdLoader.Builder(context, "ca-app-pub-3940256099942544/2247696110")
.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
// Show the ad.
NativeTemplateStyle styles = new
NativeTemplateStyle.Builder().build();
TemplateView template = ((adViewHolder) holder).Adtemplate;
template.setStyles(styles);
template.setNativeAd(unifiedNativeAd);
}
})
.withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) {
// Handle the failure by logging, altering the UI, and so on.
}
})
.withNativeAdOptions(new NativeAdOptions.Builder()
// Methods in the NativeAdOptions.Builder class can be
// used here to specify individual options settings.
.build())
.build();
adLoader.loadAd(new AdRequest.Builder().build());
}}
@Override
public int getItemCount() {
return wallpapersList.size();
}
public class WallpaperViewHolder extends RecyclerView.ViewHolder {
public ImageView thumbNail;
final ProgressBar imageloader;
public WallpaperViewHolder(View itemView) {
super(itemView);
// Grid thumbnail image view
imageloader = itemView.findViewById(R.id.imgLoader);
thumbNail = itemView
.findViewById(R.id.home_image_item_view);
}
}
class adViewHolder extends RecyclerView.ViewHolder {
TemplateView Adtemplate;
public adViewHolder(@NonNull View itemView) {
super(itemView);
Adtemplate = itemView.findViewById(R.id.my_template);
}
}
@Override
public int getItemViewType(int position) {
if ((position+1) % 5 == 0 && (position+1) != 1) {
return AD_TYPE;
}
return CONTENT_TYPE;
}
}
当我启动应用程序时,出现以下错误,该错误位于我尝试为 AD_TYPE
加载广告的 onBindViewHolder 中java.lang.NullPointerException: context cannot be null
at com.google.android.gms.common.internal.Preconditions.checkNotNull(Unknown Source:11)
at com.google.android.gms.ads.AdLoader$Builder.<init>(com.google.android.gms:play-services-ads-lite@@19.3.0:2)
at com.amatyasajal.hdwallpaperz.adapter.WallpaperzAdapter.onBindViewHolder(WallpaperzAdapter.java:104)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
请帮助我,这是我第一次使用 admob,我很困惑。提前致谢。
嘿,从你的代码来看,上下文确实是空的,你必须先在构造函数中初始化它,或者只使用 _activity 它会起作用,让我知道