android - 无法加载admob原生广告

标签 android admob native ads

当我尝试使用模板加载原生广告时,这是我使用的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ededed">

     <com.google.android.gms.ads.NativeExpressAdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="320x150"
        ads:adUnitId="@string/native_ad_exit_app">
    </com.google.android.gms.ads.NativeExpressAdView>

   <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:textSize="@dimen/title_bold"
       android:text="@string/sure_wanna_exit"
       android:textColor="@android:color/white"
       android:padding="@dimen/half_activity_horizontal_margin"
       android:gravity="center"
       android:background="@color/standard_android_blue"/>

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="@dimen/standard_button_size">

       <Button android:id="@+id/btnStayApp"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:background="@android:color/transparent"
           android:text="@string/stay"
           android:textColor="#999999"/>

       <View
           android:layout_width="2dp"
           android:layout_height="match_parent"
           android:background="@color/standard_android_blue"/>

       <Button android:id="@+id/btnExitApp"
           android:layout_width="0dp"
           android:layout_weight="1"
           android:layout_height="match_parent"
           android:background="@android:color/transparent"
           android:text="@string/exit"
           android:textColor="@color/standard_android_blue"/>

   </LinearLayout>

</LinearLayout>

这是java代码:

public class NativeAdDialog extends DialogFragment {

    /**********************/
    /** Create the dialog**/
    /**********************/

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final View v = getActivity().getLayoutInflater().inflate(R.layout.native_ad_container, null);

        /********************/
        /** Exit app button */
        /********************/

        Button exitAppButton = (Button)v.findViewById(R.id.btnExitApp);
        exitAppButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });

        /********************/
        /** Stay app button */
        /********************/

        Button stayAppButton = (Button)v.findViewById(R.id.btnStayApp);
        stayAppButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        /***********/
        /** The ad */
        /***********/

        NativeExpressAdView adView = (NativeExpressAdView) v.findViewById(R.id.adView);
        adView.loadAd(new AdRequest.Builder().build());

        return new AlertDialog.Builder(getActivity()).setView(v).create();
    }

}

失败后,我尝试设置自定义原生广告,如本项目所示: https://github.com/googleads/googleads-mobile-android-examples/tree/master/admob/NativeExample

所以我将下一个代码添加到我的 java 文件中:

/**
     * Populates a {@link NativeAppInstallAdView} object with data from a given
     * {@link NativeAppInstallAd}.
     *
     * @param nativeAppInstallAd the object containing the ad's assets
     * @param adView             the view to be populated
     */
    private void populateAppInstallAdView(NativeAppInstallAd nativeAppInstallAd,
                                          NativeAppInstallAdView adView) {
        adView.setHeadlineView(adView.findViewById(R.id.appinstall_headline));
        adView.setImageView(adView.findViewById(R.id.appinstall_image));
        adView.setBodyView(adView.findViewById(R.id.appinstall_body));
        adView.setCallToActionView(adView.findViewById(R.id.appinstall_call_to_action));
        adView.setIconView(adView.findViewById(R.id.appinstall_app_icon));
        adView.setPriceView(adView.findViewById(R.id.appinstall_price));
        adView.setStarRatingView(adView.findViewById(R.id.appinstall_stars));
        adView.setStoreView(adView.findViewById(R.id.appinstall_store));

        // Some assets are guaranteed to be in every NativeAppInstallAd.
        ((TextView) adView.getHeadlineView()).setText(nativeAppInstallAd.getHeadline());
        ((TextView) adView.getBodyView()).setText(nativeAppInstallAd.getBody());
        ((Button) adView.getCallToActionView()).setText(nativeAppInstallAd.getCallToAction());
        ((ImageView) adView.getIconView()).setImageDrawable(nativeAppInstallAd.getIcon()
                .getDrawable());

        List<NativeAd.Image> images = nativeAppInstallAd.getImages();

        if (images.size() > 0) {
            ((ImageView) adView.getImageView()).setImageDrawable(images.get(0).getDrawable());
        }

        // Some aren't guaranteed, however, and should be checked.
        if (nativeAppInstallAd.getPrice() == null) {
            adView.getPriceView().setVisibility(View.INVISIBLE);
        } else {
            adView.getPriceView().setVisibility(View.VISIBLE);
            ((TextView) adView.getPriceView()).setText(nativeAppInstallAd.getPrice());
        }

        if (nativeAppInstallAd.getStore() == null) {
            adView.getStoreView().setVisibility(View.INVISIBLE);
        } else {
            adView.getStoreView().setVisibility(View.VISIBLE);
            ((TextView) adView.getStoreView()).setText(nativeAppInstallAd.getStore());
        }

        if (nativeAppInstallAd.getStarRating() == null) {
            adView.getStarRatingView().setVisibility(View.INVISIBLE);
        } else {
            ((RatingBar) adView.getStarRatingView())
                    .setRating(nativeAppInstallAd.getStarRating().floatValue());
            adView.getStarRatingView().setVisibility(View.VISIBLE);
        }

        // Assign native ad object to the native view.
        adView.setNativeAd(nativeAppInstallAd);
    }

    /**
     * Populates a {@link NativeContentAdView} object with data from a given
     * {@link NativeContentAd}.
     */
    private void populateContentAdView(NativeContentAd nativeContentAd,
                                       NativeContentAdView adView) {
        adView.setHeadlineView(adView.findViewById(R.id.contentad_headline));
        adView.setImageView(adView.findViewById(R.id.contentad_image));
        adView.setBodyView(adView.findViewById(R.id.contentad_body));
        adView.setCallToActionView(adView.findViewById(R.id.contentad_call_to_action));
        adView.setLogoView(adView.findViewById(R.id.contentad_logo));
        adView.setAdvertiserView(adView.findViewById(R.id.contentad_advertiser));

        // Some assets are guaranteed to be in every NativeContentAd.
        ((TextView) adView.getHeadlineView()).setText(nativeContentAd.getHeadline());
        ((TextView) adView.getBodyView()).setText(nativeContentAd.getBody());
        ((TextView) adView.getCallToActionView()).setText(nativeContentAd.getCallToAction());
        ((TextView) adView.getAdvertiserView()).setText(nativeContentAd.getAdvertiser());

        List<NativeAd.Image> images = nativeContentAd.getImages();

        if (images.size() > 0) {
            ((ImageView) adView.getImageView()).setImageDrawable(images.get(0).getDrawable());
        }

        // Some aren't guaranteed, however, and should be checked.
        NativeAd.Image logoImage = nativeContentAd.getLogo();

        if (logoImage == null) {
            adView.getLogoView().setVisibility(View.INVISIBLE);
        } else {
            ((ImageView) adView.getLogoView()).setImageDrawable(logoImage.getDrawable());
            adView.getLogoView().setVisibility(View.VISIBLE);
        }

        // Assign native ad object to the native view.
        adView.setNativeAd(nativeContentAd);
    }

    /**
     * Creates a request for a new native ad based on the boolean parameters and calls the
     * corresponding "populate" method when one is successfully returned.
     */
    private void refreshAd() {

        AdLoader adLoader = new AdLoader.Builder(getActivity(), getResources().getString(R.string.native_ad_exit_app))
                .forAppInstallAd(new NativeAppInstallAd.OnAppInstallAdLoadedListener() {
                    @Override
                    public void onAppInstallAdLoaded(NativeAppInstallAd appInstallAd) {
                        // Show the app install ad.
                        NativeAppInstallAdView adView = (NativeAppInstallAdView) getActivity().getLayoutInflater()
                                .inflate(R.layout.ad_app_install, null);
                        populateAppInstallAdView(appInstallAd, adView);
                        adPlaceholder.removeAllViews();
                        adPlaceholder.addView(adView);
                    }
                })
                .forContentAd(new NativeContentAd.OnContentAdLoadedListener() {
                    @Override
                    public void onContentAdLoaded(NativeContentAd contentAd) {
                        // Show the content ad.
                        NativeContentAdView adView = (NativeContentAdView) getActivity().getLayoutInflater()
                                .inflate(R.layout.ad_content, null);
                        populateContentAdView(contentAd, adView);
                        adPlaceholder.removeAllViews();
                        adPlaceholder.addView(adView);
                    }
                })
                .withAdListener(new AdListener() {
                    @Override
                    public void onAdFailedToLoad(int errorCode) {
                        // Handle the failure by logging, altering the UI, etc.
                    }

                })
                .withNativeAdOptions(new NativeAdOptions.Builder()
                        // Methods in the NativeAdOptions.Builder class can be
                        // used here to specify individual options settings.
                        .build())
                .build();

        adLoader.loadAd(new AdRequest.Builder().build());

}

我在 onCreate 方法上调用refreshAd,但 adLoader.isLoading() 始终返回 false。

我做错了什么?

最佳答案

ads:adSize="320x150"仅适用于 small and medium template size native ads

更安全的测试方法是使用 ads:adSize="FULL_WIDTHx250",它支持所有三种模板尺寸。 FULL_WIDTH 不是强制性的,但我在这里使用它是为了向您展示可能性。您可以选择 ads:adSize="280x250"以支持所有模板尺寸。

在 AdMob 中,当您为原生广告创建新的广告单元时,请确保选择正确的尺寸,因为该尺寸将来无法修改。等待几个小时,让 AdMob 开始向新创建的广告单元 ID 转换广告。 一旦一切正常,您就可以更改广告的尺寸以最符合您的设计要求。 AdMob - Create a new AD Unit for native ad

关于android - 无法加载admob原生广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37380056/

相关文章:

android - 无法在 Android 项目中加载预构建的 OpenSSL 库

android - 在CursorAdapter中滚动会导致崩溃应用程序

javascript - 相机功能不适用于 cordova.js/camera.js

android - RxJava Flowable 在 Room db 中的数据发生变化时不调用 Subscriber onNext

android - Admob,填充率真的很差

ios - 单击横幅广告时的 AdMob 事件

android - 无法将视频共享到Youtube,这些视频位于/mnt/sdcard/Android/sd卡中的数据中

iphone - 禁用 ARC...(AdMob SDK)

android - 在 Android 上的 Chrome 中禁用 native 延迟加载图像

android - 无法解析仅在 Windows 操作系统中显示的相应 JNI 函数