加载原生 admob 广告时出现问题。无广告配置或无效模板错误
Problem with loading native admob ads. No ads config or Invalid Template error
我正在尝试加载 admob 原生广告,但我做不到。我看不出问题出在哪里。找了3天不知道问题出在哪里...这是加载广告的代码:
private void getNativeAds() {
_nativeAdmobMutable = ViewModelProviders.of(this).get(MutableNativeADModel.class);
AdLoader.Builder builder = new AdLoader.Builder(this, GlobalConstants.numberForNativeAdmob);
adLoader = builder.forUnifiedNativeAd(
new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
_nativeAds.add(unifiedNativeAd);
if (!adLoader.isLoading()) {
_nativeAdmobMutable.setAdmobNativeAd(_nativeAds);
}
if (isDestroyed()) {
unifiedNativeAd.destroy();
return;
}
}
}).withAdListener(
new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
}
@Override
public void onAdFailedToLoad(LoadAdError errorCode) {
Log.d("ERROR ", errorCode.getMessage());
if (!adLoader.isLoading()) {
_nativeAdmobMutable.setAdmobNativeAd(_nativeAds);
}
}
}).withNativeAdOptions(new NativeAdOptions.Builder()
.build())
.build();
adLoader.loadAds(new AdRequest.Builder().build(), 5);
}
此方法在
之后被调用
MobileAds.initialize(this, new OnInitializationCompleteListener() ... etc
这是我的依赖项
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.exoplayer:exoplayer:2.10.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha05'
implementation 'com.intuit.sdp:sdp-android:1.0.6'
implementation 'com.google.android.gms:play-services-ads:19.4.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.picasso:picasso:2.71828'
每次使用日志调用 onAdFailedToLoad 回调:
I/Ads: Ad failed to load : 0
D/ERROR: Invalid template ID: -1
我还尝试加载单个广告,例如:
adLoader.loadAd(new AdRequest.Builder().build());
然后我收到错误 无广告配置。 和 I/Ads:广告加载失败:3
有谁知道这里的问题是什么?我还实现了横幅、插屏和视频奖励,它们工作得很好(应用程序在 GP 上运行了将近一年)。当然,我使用的是 TEST ID 广告,而不是真实的。提前致谢!
编辑 1:
这是布局
<com.google.android.gms.ads.formats.UnifiedNativeAdView
android:id="@+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#FFFFFF"
android:minHeight="50dp"
android:orientation="vertical">
<TextView
android:id="@+id/ad_attribution"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:text="Ad"
android:background="#FFCC66"
android:width="15dp"
android:height="15dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/ad_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:paddingBottom="5dp"
android:paddingRight="5dp"
android:paddingEnd="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/ad_headline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textSize="16sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/ad_advertiser"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:textSize="14sp"
android:textStyle="bold"/>
<RatingBar
android:id="@+id/ad_stars"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:numStars="5"
android:stepSize="0.5" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/ad_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:textSize="12sp" />
<com.google.android.gms.ads.formats.MediaView
android:id="@+id/ad_media"
android:layout_gravity="center_horizontal"
android:layout_width="250dp"
android:layout_height="175dp"
android:layout_marginTop="5dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="@+id/ad_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingStart="5dp"
android:paddingRight="5dp"
android:paddingEnd="5dp"
android:textSize="12sp" />
<TextView
android:id="@+id/ad_store"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingStart="5dp"
android:paddingRight="5dp"
android:paddingEnd="5dp"
android:textSize="12sp" />
<Button
android:id="@+id/ad_call_to_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</com.google.android.gms.ads.formats.UnifiedNativeAdView>
正在加载我在上面写的广告。
这是如何连接布局
public UnifiedNativeAdViewHolder(@NonNull View itemView) {
super(itemView);
adView = (UnifiedNativeAdView) itemView.findViewById(R.id.ad_view);
// The MediaView will display a video asset if one is present in the ad, and the
// first image asset otherwise.
adView.setMediaView((MediaView) adView.findViewById(R.id.ad_media));
// Register the view used for each individual asset.
adView.setHeadlineView(adView.findViewById(R.id.ad_headline));
adView.setBodyView(adView.findViewById(R.id.ad_body));
adView.setCallToActionView(adView.findViewById(R.id.ad_call_to_action));
adView.setIconView(adView.findViewById(R.id.ad_icon));
adView.setPriceView(adView.findViewById(R.id.ad_price));
adView.setStarRatingView(adView.findViewById(R.id.ad_stars));
adView.setStoreView(adView.findViewById(R.id.ad_store));
adView.setAdvertiserView(adView.findViewById(R.id.ad_advertiser));
}
并像这样填充
// Some assets are guaranteed to be in every UnifiedNativeAd.
((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
((TextView) adView.getBodyView()).setText(nativeAd.getBody());
((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());
// These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
// check before trying to display them.
NativeAd.Image icon = nativeAd.getIcon();
if (icon == null) {
adView.getIconView().setVisibility(View.INVISIBLE);
} else {
((ImageView) adView.getIconView()).setImageDrawable(icon.getDrawable());
adView.getIconView().setVisibility(View.VISIBLE);
}
if (nativeAd.getPrice() == null) {
adView.getPriceView().setVisibility(View.INVISIBLE);
} else {
adView.getPriceView().setVisibility(View.VISIBLE);
((TextView) adView.getPriceView()).setText(nativeAd.getPrice());
}
if (nativeAd.getStore() == null) {
adView.getStoreView().setVisibility(View.INVISIBLE);
} else {
adView.getStoreView().setVisibility(View.VISIBLE);
((TextView) adView.getStoreView()).setText(nativeAd.getStore());
}
if (nativeAd.getStarRating() == null) {
adView.getStarRatingView().setVisibility(View.INVISIBLE);
} else {
((RatingBar) adView.getStarRatingView())
.setRating(nativeAd.getStarRating().floatValue());
adView.getStarRatingView().setVisibility(View.VISIBLE);
}
if (nativeAd.getAdvertiser() == null) {
adView.getAdvertiserView().setVisibility(View.INVISIBLE);
} else {
((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser());
adView.getAdvertiserView().setVisibility(View.VISIBLE);
}
// Assign native ad object to the native view.
adView.setNativeAd(nativeAd);
您是否尝试重建项目?
您能否也post您的原生广告布局,以及您如何扩充和填充布局?
我面临同样的问题,我能够获得原生广告的唯一方法是不使用测试广告 ID。通过测试 ID,我得到 No ads config 和 I/Ads: Ad failed to load : 3。这与我使用的两个原生测试广告 ID 中的哪一个无关。使用真实广告 ID 并添加测试单元 ID,我得到了测试广告,但没有媒体。如果没有测试单元 ID,我有时会从 Facebook 中介获得测试广告,有时会收到 无效模板 ID:-1.
编辑 2021 年 9 月
在 documentation for the test ad Ids 中添加了关于测试广告 ID 和使用 app-ads.txt 文件的警告。显然,如果您有 app-ads.txt,则需要向其中添加这一行才能使用演示广告 ID:
google.com, pub-3940256099942544, DIRECT, f08c47fec0942fa0
我想这就是我的问题,我从来没有让测试广告 ID 起作用,不得不将我的 phone 添加为测试设备。
我正在尝试加载 admob 原生广告,但我做不到。我看不出问题出在哪里。找了3天不知道问题出在哪里...这是加载广告的代码:
private void getNativeAds() {
_nativeAdmobMutable = ViewModelProviders.of(this).get(MutableNativeADModel.class);
AdLoader.Builder builder = new AdLoader.Builder(this, GlobalConstants.numberForNativeAdmob);
adLoader = builder.forUnifiedNativeAd(
new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
_nativeAds.add(unifiedNativeAd);
if (!adLoader.isLoading()) {
_nativeAdmobMutable.setAdmobNativeAd(_nativeAds);
}
if (isDestroyed()) {
unifiedNativeAd.destroy();
return;
}
}
}).withAdListener(
new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
}
@Override
public void onAdFailedToLoad(LoadAdError errorCode) {
Log.d("ERROR ", errorCode.getMessage());
if (!adLoader.isLoading()) {
_nativeAdmobMutable.setAdmobNativeAd(_nativeAds);
}
}
}).withNativeAdOptions(new NativeAdOptions.Builder()
.build())
.build();
adLoader.loadAds(new AdRequest.Builder().build(), 5);
}
此方法在
之后被调用MobileAds.initialize(this, new OnInitializationCompleteListener() ... etc
这是我的依赖项
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.exoplayer:exoplayer:2.10.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha05'
implementation 'com.intuit.sdp:sdp-android:1.0.6'
implementation 'com.google.android.gms:play-services-ads:19.4.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.picasso:picasso:2.71828'
每次使用日志调用 onAdFailedToLoad 回调:
I/Ads: Ad failed to load : 0
D/ERROR: Invalid template ID: -1
我还尝试加载单个广告,例如:
adLoader.loadAd(new AdRequest.Builder().build());
然后我收到错误 无广告配置。 和 I/Ads:广告加载失败:3
有谁知道这里的问题是什么?我还实现了横幅、插屏和视频奖励,它们工作得很好(应用程序在 GP 上运行了将近一年)。当然,我使用的是 TEST ID 广告,而不是真实的。提前致谢!
编辑 1: 这是布局
<com.google.android.gms.ads.formats.UnifiedNativeAdView
android:id="@+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#FFFFFF"
android:minHeight="50dp"
android:orientation="vertical">
<TextView
android:id="@+id/ad_attribution"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:text="Ad"
android:background="#FFCC66"
android:width="15dp"
android:height="15dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/ad_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:paddingBottom="5dp"
android:paddingRight="5dp"
android:paddingEnd="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/ad_headline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textSize="16sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/ad_advertiser"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:textSize="14sp"
android:textStyle="bold"/>
<RatingBar
android:id="@+id/ad_stars"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:numStars="5"
android:stepSize="0.5" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/ad_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:textSize="12sp" />
<com.google.android.gms.ads.formats.MediaView
android:id="@+id/ad_media"
android:layout_gravity="center_horizontal"
android:layout_width="250dp"
android:layout_height="175dp"
android:layout_marginTop="5dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="@+id/ad_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingStart="5dp"
android:paddingRight="5dp"
android:paddingEnd="5dp"
android:textSize="12sp" />
<TextView
android:id="@+id/ad_store"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingStart="5dp"
android:paddingRight="5dp"
android:paddingEnd="5dp"
android:textSize="12sp" />
<Button
android:id="@+id/ad_call_to_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</com.google.android.gms.ads.formats.UnifiedNativeAdView>
正在加载我在上面写的广告。 这是如何连接布局
public UnifiedNativeAdViewHolder(@NonNull View itemView) {
super(itemView);
adView = (UnifiedNativeAdView) itemView.findViewById(R.id.ad_view);
// The MediaView will display a video asset if one is present in the ad, and the
// first image asset otherwise.
adView.setMediaView((MediaView) adView.findViewById(R.id.ad_media));
// Register the view used for each individual asset.
adView.setHeadlineView(adView.findViewById(R.id.ad_headline));
adView.setBodyView(adView.findViewById(R.id.ad_body));
adView.setCallToActionView(adView.findViewById(R.id.ad_call_to_action));
adView.setIconView(adView.findViewById(R.id.ad_icon));
adView.setPriceView(adView.findViewById(R.id.ad_price));
adView.setStarRatingView(adView.findViewById(R.id.ad_stars));
adView.setStoreView(adView.findViewById(R.id.ad_store));
adView.setAdvertiserView(adView.findViewById(R.id.ad_advertiser));
}
并像这样填充
// Some assets are guaranteed to be in every UnifiedNativeAd.
((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
((TextView) adView.getBodyView()).setText(nativeAd.getBody());
((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());
// These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
// check before trying to display them.
NativeAd.Image icon = nativeAd.getIcon();
if (icon == null) {
adView.getIconView().setVisibility(View.INVISIBLE);
} else {
((ImageView) adView.getIconView()).setImageDrawable(icon.getDrawable());
adView.getIconView().setVisibility(View.VISIBLE);
}
if (nativeAd.getPrice() == null) {
adView.getPriceView().setVisibility(View.INVISIBLE);
} else {
adView.getPriceView().setVisibility(View.VISIBLE);
((TextView) adView.getPriceView()).setText(nativeAd.getPrice());
}
if (nativeAd.getStore() == null) {
adView.getStoreView().setVisibility(View.INVISIBLE);
} else {
adView.getStoreView().setVisibility(View.VISIBLE);
((TextView) adView.getStoreView()).setText(nativeAd.getStore());
}
if (nativeAd.getStarRating() == null) {
adView.getStarRatingView().setVisibility(View.INVISIBLE);
} else {
((RatingBar) adView.getStarRatingView())
.setRating(nativeAd.getStarRating().floatValue());
adView.getStarRatingView().setVisibility(View.VISIBLE);
}
if (nativeAd.getAdvertiser() == null) {
adView.getAdvertiserView().setVisibility(View.INVISIBLE);
} else {
((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser());
adView.getAdvertiserView().setVisibility(View.VISIBLE);
}
// Assign native ad object to the native view.
adView.setNativeAd(nativeAd);
您是否尝试重建项目?
您能否也post您的原生广告布局,以及您如何扩充和填充布局?
我面临同样的问题,我能够获得原生广告的唯一方法是不使用测试广告 ID。通过测试 ID,我得到 No ads config 和 I/Ads: Ad failed to load : 3。这与我使用的两个原生测试广告 ID 中的哪一个无关。使用真实广告 ID 并添加测试单元 ID,我得到了测试广告,但没有媒体。如果没有测试单元 ID,我有时会从 Facebook 中介获得测试广告,有时会收到 无效模板 ID:-1.
编辑 2021 年 9 月 在 documentation for the test ad Ids 中添加了关于测试广告 ID 和使用 app-ads.txt 文件的警告。显然,如果您有 app-ads.txt,则需要向其中添加这一行才能使用演示广告 ID:
google.com, pub-3940256099942544, DIRECT, f08c47fec0942fa0
我想这就是我的问题,我从来没有让测试广告 ID 起作用,不得不将我的 phone 添加为测试设备。