【问题标题】:Issue with Admob NativeExpressAdView in Recyclerview ImplementationRecyclerview 实施中的 Admob NativeExpressAdView 问题
【发布时间】:2017-09-01 15:51:55
【问题描述】:

在这里问这个问题之前,我已经做了很多谷歌搜索。

我已将 NativeExpressAdView 集成到我的 Recyclerview 中,如google example 中所述,它也可以正常工作。但问题是,在这个示例项目中,他们从零位置添加 NativeExpressAdView,但不想在零位置添加 NativeExpressAdView。

我也可以在随机位置添加,但是当我们必须从代码中为 NativeExpressAdView 设置广告尺寸时就会出现问题。

MainActivitysetUpAndLoadNativeExpressAds方法中访问的Cardview将为Null,如果不在第0个位置添加视图将给出NPE。

这是我修改后的代码sn-p

/**
     * 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.
            }
        });
    }

即使我在从 repo 中提取示例项目代码后实施相同的更改,也会出现此问题。

这是我的 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)

这是某种错误还是我做错了什么。

【问题讨论】:

    标签: android android-recyclerview admob google-admob native-ads


    【解决方案1】:

    只需再添加一个条件

    if (i!=0){}
    

    在两个 for 循环中
    更新

      */
    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.
            }
        });
    }
    

    【讨论】:

    • 你的意思是说我只从 i=0 开始初始化 for 循环并添加 if 条件 if i != 0 添加 NativeExpressAdView 否则不?
    • 是的,你必须从 0 开始,但根据条件,这可以避免索引 0
    • 尝试给出静态尺寸
    • 即使我也面临同样的问题,如果您解决了问题,请发布答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2022-11-06
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多