gpt4 book ai didi

android - 加载原生 admob 广告时出现问题。没有广告配置或无效模板错误

转载 作者:行者123 更新时间:2023-12-03 23:43:55 25 4
gpt4 key购买 nike

我正在尝试加载 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);

最佳答案

您是否还可以发布您的原生广告布局,以及您如何膨胀和填充布局?
我面临同样的问题,我能够获得原生广告的唯一方法是不使用测试广告 ID。使用测试 ID,我得到 无广告配置 I/Ads:广告加载失败: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 工作,而不得不将我的手机添加为测试设备。

关于android - 加载原生 admob 广告时出现问题。没有广告配置或无效模板错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64100766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com