I'm trying to add an Admob banner in a recyclerview.
My problem is to initialize the Admob.
I have 2 views: the Adview and the list.xml. Adview has Admob xml banner on it.
Adapter
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
if (viewType == ad) {
ad_act = 1;
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.adview, parent, false);
}
else {
ad_act = 0;
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list, parent, false);
}
return new ViewHolder(v);
}
I need to initialize the Admob, so I tried to add it in my main activity not in my adapter:
Main Activity inside onCreate:
ViewGroup content = (ViewGroup) findViewById(R.id.content_frame);
getLayoutInflater().inflate(R.layout.activity_main, content, true);
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
but this gives me a null exception R.id.adView
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
since I'm not adding the r.layout.adview in the main_activity. Any ideas how can I add it there?