Facebook Native Ads Manager Android广告nativeAd.isAdInvalidated()返回true

时间:2019-02-17 06:48:41

标签: android facebook admob facebook-audience-network native-ads

nativeAd.isAdInvalidated()对于刚刚加载的广告返回true

我既在点击按钮时使用了插页式广告,也在回收者视图中使用了原生广告

MainActivity.java

NativeAdsManager listNativeAdsManager;

@Override
protected void onStart() {
    super.onStart();

   // Initialize the Audience Network SDK
    AudienceNetworkAds.initialize(this);

    ... // interstitialAdFromFacebook works fine


        // Native Facebook ads
    listNativeAdsManager = new NativeAdsManager(this,fb_native,10);
    listNativeAdsManager.loadAds(NativeAd.MediaCacheFlag.ALL);
}


NativeAd loadAndShowNativeAd(){
     //calling this method from adapter Class.

        try {
            return listNativeAdsManager.nextNativeAd();
        } catch (Exception E){
            return null;
        }
}

void reloadNativeAds(){
 //this is also called from adapter class.
        try {
            listNativeAdsManager.loadAds(NativeAd.MediaCacheFlag.ALL);

        } catch (Exception E){

        }
}

recyclerviewAdapter.java

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    ...
  // loading ad on every 7th recyclerview Item
  if (position %7 == 0 && position !=0){
        myHolder.ad_unit.setVisibility(View.VISIBLE);

        try{


        NativeAd nativeAd = ((MainActivity)mContext).loadAndShowNativeAd();

如果我从以下条件中删除了&& !nativeAd.isAdInvalidated(),则广告会完美展示。

        if (nativeAd != null && nativeAd.isAdLoaded() && !nativeAd.isAdInvalidated()) {
            //show ad
            nativeAd.unregisterView();

            // Add the Ad view into the ad container.
            NativeAdLayout nativeAdLayout = myHolder.ad_unit;
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // Inflate the Ad view.  The layout referenced should be the one you created in the last step.
            LinearLayout adView = (LinearLayout) inflater.inflate(R.layout.native_ad_layout, nativeAdLayout, false);
            nativeAdLayout.addView(adView);

            // Add the AdOptionsView

            LinearLayout adChoicesContainer = adView.findViewById(R.id.ad_choices_container);
            AdOptionsView adOptionsView = new AdOptionsView(((MainActivity)mContext), nativeAd, nativeAdLayout);
            adChoicesContainer.removeAllViews();
            adChoicesContainer.addView(adOptionsView, 0);

            // Create native UI using the ad metadata.
            AdIconView nativeAdIcon = adView.findViewById(R.id.native_ad_icon);
            TextView nativeAdTitle = adView.findViewById(R.id.native_ad_title);
            MediaView nativeAdMedia = adView.findViewById(R.id.native_ad_media);
            TextView nativeAdSocialContext = adView.findViewById(R.id.native_ad_social_context);
            TextView nativeAdBody = adView.findViewById(R.id.native_ad_body);
            TextView sponsoredLabel = adView.findViewById(R.id.native_ad_sponsored_label);
            Button nativeAdCallToAction = adView.findViewById(R.id.native_ad_call_to_action);

            // Set the Text.
            nativeAdTitle.setText(nativeAd.getAdvertiserName());
            nativeAdBody.setText(nativeAd.getAdBodyText());
            nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
            nativeAdCallToAction.setVisibility(nativeAd.hasCallToAction() ? View.VISIBLE : View.INVISIBLE);
            nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
            sponsoredLabel.setText(nativeAd.getSponsoredTranslation());

            // Create a list of clickable views
            List<View> clickableViews = new ArrayList<>();
            clickableViews.add(nativeAdTitle);
            clickableViews.add(nativeAdCallToAction);

            // Register the Title and CTA button to listen for clicks.
            nativeAd.registerViewForInteraction(
                    adView,
                    nativeAdMedia,
                    nativeAdIcon,
                    clickableViews);


        } else if (nativeAd.isAdInvalidated()){
            ((MainActivity)mContext).reloadNativeAds();
            Toast.makeText((MainActivity)mContext, "nativeAd.isAdLoaded() : "+nativeAd.isAdLoaded()+" isAdInvalidated()", Toast.LENGTH_SHORT).show();

        }

        } catch (Exception e){

        }


    } else {
        myHolder.ad_unit.removeAllViewsInLayout();
        myHolder.ad_unit.setVisibility(View.GONE);
    }

}//onBindViewHolder

我不明白我在想什么。

1 个答案:

答案 0 :(得分:1)

该错误已在5.2.1中修复。这是变更日志: https://developers.facebook.com/docs/audience-network/changelog-android#5_2_1