我正在尝试实现原生广告。我在模拟器上尝试时,在测试单元和真实的单元上都得到了错误代码0和3。在检查日志时,我发现了以下语句。Received log message: <Google:HTML> Incorrect native ad response. Click actions were not properly specified
Ad failed to load : 0
当我使用真实的广告单元时,错误代码0出现。
我正在尝试在FrameLayout中填充这个原生广告。下面是我的xml文件中的代码片段:
<LinearLayout
android:id="@+id/adLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:orientation="vertical">
<FrameLayout
android:id="@+id/nativeAdArea"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
这是我的java代码片段。
FrameLayout nativeAdArea;
nativeAdArea = findViewById(R.id.nativeAdArea);
private void showNativeBanner() {
AdLoader adLoader = new AdLoader.Builder(this, getResources().getString(R.string.admob_native_mainactivity))
.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
Log.e("nativead", "loaded");
// Show the ad.
nativeAdArea.setVisibility(View.VISIBLE);
UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater()
.inflate(R.layout.layout_homepage_nativead, null);
// This method sets the text, images and the native ad, etc into the ad
// view.
mUnifiedNativeAd = populateUnifiedNativeAdView(unifiedNativeAd, adView);
nativeAdArea.removeAllViews();
nativeAdArea.addView(adView);
}
})
.withAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
mUnifiedNativeAd.setOnPaidEventListener(new OnPaidEventListener() {
@Override
public void onPaidEvent(AdValue adValue) {
Bundle params = new Bundle();
params.putString("valuemicros", String.valueOf(adValue.getValueMicros()));
params.putString("currency", adValue.getCurrencyCode());
params.putString("precision", String.valueOf(adValue.getPrecisionType()));
params.putString("adunitid", getResources().getString(R.string.admob_native_mainactivity));
params.putString("network", Objects.requireNonNull(mUnifiedNativeAd.getResponseInfo()).getMediationAdapterClassName());
AnalyticsManager.logEvent("paid_ad_impression", params);
}
});
}
@Override
public void onAdFailedToLoad(int errorCode) {
Log.e("nativead", "failedtoload" + errorCode);
// Handle the failure by logging, altering the UI, and so on.
nativeAdArea.setVisibility(View.GONE);
// getSmaatoNativeAd();
}
@Override
public void onAdClicked() {
if (mFirebaseAnalytics == null) {
mFirebaseAnalytics = FirebaseAnalytics.getInstance(NewMainActivity.this);
}
Bundle adClickAnalyticsbundle = new Bundle();
adClickAnalyticsbundle.putString("app_version", BuildConfig.VERSION_NAME);
adClickAnalyticsbundle.putString("type", "native");
adClickAnalyticsbundle.putString("ad_unit_name", "homepage_lower_native");
mFirebaseAnalytics.logEvent("clicked_ad", adClickAnalyticsbundle);
Bundle abTestBundle = new Bundle();
abTestBundle.putString("app_version", BuildConfig.VERSION_NAME);
abTestBundle.putString("type", "native");
mFirebaseAnalytics.logEvent("native_vs_adaptive", abTestBundle);
}
})
.withNativeAdOptions(new NativeAdOptions.Builder()
// Methods in the NativeAdOptions.Builder class can be
// used here to specify individual options settings.
.build())
.build();
AdRequest adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class, extras)
.build();
runOnUiThread(() -> {
adLoader.loadAd(adRequest);
});
}
```
[![Here's a snapshot of the log][1]][1]
[1]: https://i.stack.imgur.com/f43d9.png
2条答案
按热度按时间cnwbcb6i1#
我也面临着同样的问题模拟器,但当我尝试相同的代码在真实的的设备上原生广告似乎工作完美。
carvr3hs2#
我有同样的问题,并找到了另一个正确的答案:Flutter, Native Admob, Ad failed to load : 0
您需要使用安装了Google Play框架的模拟器来测试您的应用。