I am developing an application that watches videos. My app has a list videos and paging data. Everything is ok until I add native ads to list. I have done as following code:
recyclerView.post(new Runnable() { @Override public void run() { final float scale = ListMostPopularByCountryActivity.this.getResources().getDisplayMetrics().density; // Set the ad size and ad unit ID for each Native Express ad in the items list. for (int i = 0; i < mListVideo.size(); i += ITEMS_PER_AD) { if ((mListVideo.get(i) instanceof NativeExpressAdView)) { final NativeExpressAdView adView = (NativeExpressAdView) mListVideo.get(i); final CardView cardView = (CardView) findViewById(R.id.native_ads_card_view); if (ViewCompat.isLaidOut(cardView)) { final int adWidth = cardView.getWidth() - cardView.getPaddingLeft() - cardView.getPaddingRight(); AdSize adSize = new AdSize((int) (adWidth / scale), NATIVE_EXPRESS_AD_HEIGHT); adView.setAdSize(adSize); adView.setAdUnitId(Constant.ADS_NATIVE_ID); } } } // Load the first Native Express ad in the items list. loadNativeExpressAd(0); } });
The code add native ads item to list data:
private void addNativeExpressAds() { // Loop through the items array and place a new Native Express ad in every ith position in // the items List. for (int i = 0; i < mListVideo.size(); i += ITEMS_PER_AD) { final NativeExpressAdView adView = new NativeExpressAdView(ListMostPopularByCountryActivity.this); if (!(mListVideo.get(i) instanceof NativeExpressAdView)) { mListVideo.add(i, adView); } } }
The first time, it displayed well (contains video item and native ads). But when load more (go to next page), it got error by the line of code (cardView is null):
final CardView cardView = (CardView) findViewById(R.id.native_ads_card_view);
I have debugged and found that childview of recyclerView don't have native ads item although list data has it. What is wrong with this?