0

J'ai déjà fait beaucoup de recherche avant de poser cette question ici.Problème avec Admob NativeExpressAdView dans l'implémentation de Recyclerview

J'ai intégré NativeExpressAdView dans mon Recyclerview comme décrit ici dans ce google example et il fonctionne bien aussi. Mais le problème est, dans cet exemple de projet, ils ajoutent NativeExpressAdView à partir de la position zéro, mais ne veulent pas ajouter NativeExpressAdView à la position zéro.

Je peux aussi ajouter une position aléatoire, mais un problème survient lorsque nous devons définir l'adsize pour le code NativeExpressAdView.

Cardview, accessible en MainActivity par la méthode setUpAndLoadNativeExpressAds, sera nul et donnera NPE si aucune vue n'est ajoutée à la position zéro.

Voici mon code modifié snippet

/** 
    * Adds Native Express ads to the items list. 
    */ 
    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 = 7; i <= mRecyclerViewItems.size(); i += ITEMS_PER_AD) { 
      final NativeExpressAdView adView = new NativeExpressAdView(MainActivity.this); 
      mRecyclerViewItems.add(i, adView); 
     } 
    } 

    /** 
    * Sets up and loads the Native Express ads. 
    */ 
    private void setUpAndLoadNativeExpressAds() { 
     // Use a Runnable to ensure that the RecyclerView has been laid out before setting the 
     // ad size for the Native Express ad. This allows us to set the Native Express ad's 
     // width to match the full width of the RecyclerView. 
     mRecyclerView.post(new Runnable() { 
      @Override 
      public void run() { 
       final float scale = MainActivity.this.getResources().getDisplayMetrics().density; 
       // Set the ad size and ad unit ID for each Native Express ad in the items list. 
       for (int i = 7; i <= mRecyclerViewItems.size(); i += ITEMS_PER_AD) { 
        final NativeExpressAdView adView = 
          (NativeExpressAdView) mRecyclerViewItems.get(i); 
        final CardView cardView = (CardView) findViewById(R.id.ad_card_view); 
        final int adWidth = cardView.getWidth() - cardView.getPaddingLeft() 
          - cardView.getPaddingRight(); //Here cardView will be Null (so NPE). 
        AdSize adSize = new AdSize((int) (adWidth/scale), NATIVE_EXPRESS_AD_HEIGHT); 
        adView.setAdSize(adSize); 
        adView.setAdUnitId(AD_UNIT_ID); 
       } 

       // Load the first Native Express ad in the items list. 
       loadNativeExpressAd(7); //Not added code for this method as nothing is modified in that. 
      } 
     }); 
    } 

Cette question arrive même si je mets en œuvre les modifications mêmes après avoir tiré le code exemple de projet de mise en pension.

Voici mon accident logcat

FATAL EXCEPTION: main 
java.lang.NullPointerException                               at com.google.android.gms.example.nativeexpressrecyclerviewexample.MainActivity$1.run(MainActivity.java:106) 
at android.os.Handler.handleCallback(Handler.java:725) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5041) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
at dalvik.system.NativeStart.main(Native Method) 

Alors est-ce une sorte de bug ou je fais quelque chose de mal.

Répondre

0

juste ajouter une condition que

if (i!=0){} 

dans la fois pour la boucle
mise à jour:

*/ 
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 <= mRecyclerViewItems.size(); i += ITEMS_PER_AD) { 
if(i!=0){ 
     final NativeExpressAdView adView = new NativeExpressAdView(MainActivity.this); 
     mRecyclerViewItems.add(i, adView); 
    }} 
} 

/** 
* Sets up and loads the Native Express ads. 
*/ 
private void setUpAndLoadNativeExpressAds() { 
    // Use a Runnable to ensure that the RecyclerView has been laid out before setting the 
    // ad size for the Native Express ad. This allows us to set the Native Express ad's 
    // width to match the full width of the RecyclerView. 
    mRecyclerView.post(new Runnable() { 
     @Override 
     public void run() { 
      final float scale = MainActivity.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 <= mRecyclerViewItems.size(); i += ITEMS_PER_AD) { 
if(i!=0){ 
       final NativeExpressAdView adView = 
         (NativeExpressAdView) mRecyclerViewItems.get(i); 
       final CardView cardView = (CardView) findViewById(R.id.ad_card_view); 
       final int adWidth = cardView.getWidth() - cardView.getPaddingLeft() 
         - cardView.getPaddingRight(); //Here cardView will be Null (so NPE). 
       AdSize adSize = new AdSize((int) (adWidth/scale), NATIVE_EXPRESS_AD_HEIGHT); 
       adView.setAdSize(adSize); 
       adView.setAdUnitId(AD_UNIT_ID); 
      }} 

      // Load the first Native Express ad in the items list. 
      loadNativeExpressAd(ITEMS_PER_AD); //Not added code for this method as nothing is modified in that. 
     } 
    }); 
} 
+0

vous dire que je continue l'initialisation de la boucle de i = 0 et y ajouter si la condition que si i! = 0 ajouter le NativeExpressAdView else pas? –

+0

oui vous devez commencer par 0 mais par condition cela éviter l'index 0 –

+0

essayez ceci peut être utile cela fonctionne pour moi –