i'm trying to load admob native ads inside a recyclerview. I've used the native ads templates. I've initialized the ads in the main activity(onCreate).
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
this is my 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" />
and this is my adapter class
public class WallpaperzAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
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;
}
}
when i start the app, i get the following error which is the located in the onBindViewHolder where i tried to load the ad for the AD_TYPE
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)
please help me, this is my first time working with admob and I'm very confused. Thanks in advance.
hey from your code the context is really null you have to initialize it first in constractor or just use _activity it will work and let me know