2016-05-22 6 views
2

をロードできません:が、これは私が使用するXMLコードでのAdMobネイティブ広告

<?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()); 

} 

私はrefreshAd on onCreateメソッドを呼び出しますが、adLoader.isLoading()は常にfalseを返します。

私は間違っていますか?

+0

こんにちは@SUXAあなたの問題の解決策を見つけましたか? –

+0

いいえ、私はインタースティシャル広告を代わりに使用しています。 – SUXA

答えて

2

広告:すべての3つのテンプレートサイズをサポートしていadSize =「FULL_WIDTHx250」:adSizeは=「320x150」がテストする安全な方法で広告を使用することであろうだけsmall and medium template size native ads

で動作します。 FULL_WIDTHは必須ではありませんが、ここでは可能性を示すために使用しています。すべてのテンプレートサイズをサポートする代わりに、広告:adSize = "280x250"を選択できます。

ネイティブ広告用の新しい広告ユニットを作成する際には、AdMobで適切なサイズを選択してください。これは今後変更できません。 AdMobが新しく作成した広告ユニットIDに広告を配信するまでに数時間かかることがあります。 すべての作業が完了したら、設計要件に最も適合するように広告のサイズを変更できます。 AdMob - Create a new AD Unit for native ad

+0

最後に誰かが問題を発見しました。私のネイティブ広告のサイズは大きく、私は320x150を使用しました。 FULL_WIDTHx250も機能しませんが、280x250はうまく機能します。どうもありがとうございました。ところで、大きなネイティブ広告の幅と高さはどれくらいですか? – SUXA

+0

あなたのアプリに直接依存するので、最適なサイズはありません。私はあなたのアプリでどのサイズが最もよく見えるのかを意味します。テンプレートサイズの最小および最大の制限を考慮することは賢明に選択してください。 – LokiDroid

関連する問題