我正在try 使用XML格式显示AdMob原生广告,并在Jetpack Compose中使用androidViewBinding.广告根本没有显示,但我没有收到任何有用的错误消息.

onAdFailedToLoad以内,我只得到"内部错误."例外.

对于xml本身,当我验证文件时,我得到"错误:(5,42)cvc-elt.1.a:找不到元素‘com.google.android.gms.ads.nativead.NativeAdView’."的声明.

我在build.gradle文件中启用了viewBinding.已添加所有必要的依赖项.

这是可组合函数:

@Composable
fun NativeAdView(adUnitId: String) {
    Box(modifier = Modifier.defaultMinSize(minHeight = 200.dp)) {
        AndroidViewBinding(
            factory = { inflater, parent, attachToParent ->
                val binding = NativeAdViewBinding.inflate(inflater, parent, attachToParent)
                val adView = binding.root.also { adView ->
                    adView.headlineView = binding.adHeadline
                    adView.iconView = binding.adAppIcon
                }
                try {
                    val adLoader = AdLoader.Builder(
                        adView.context,
                        adUnitId,
                    )
                        .forNativeAd { nativeAd ->
                            nativeAd.icon?.let {
                                binding.adAppIcon.setImageDrawable(it.drawable)
                                binding.adAppIcon.isVisible = true
                            }
                            nativeAd.headline?.let {
                                if (it.isNotBlank()) {
                                    binding.adHeadline.text = it
                                    binding.adHeadline.isVisible = true
                                }
                            }

                            adView.setNativeAd(nativeAd)
                        }
                        .withAdListener(
                            object : AdListener() {
                                override fun onAdFailedToLoad(error: LoadAdError) {
                                    super.onAdFailedToLoad(error)

                                }
                            },
                        )
                        .withNativeAdOptions(NativeAdOptions.Builder().build())
                        .build()
                    adLoader.loadAd(AdRequest.Builder().build())
                } catch (e: Exception) {
                    Log.e("Exception", e.message.toString())
                }
                binding
            },
        )
    }
}

这是UI:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.nativead.NativeAdView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test ad" />

        <ImageView
            android:id="@+id/ad_app_icon"
            android:layout_width="52dp"
            android:layout_height="52dp"
            android:adjustViewBounds="true"
            android:contentDescription="ad_icon" />

        <TextView
            android:id="@+id/ad_headline"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp" />
    </LinearLayout>
</com.google.android.gms.ads.nativead.NativeAdView>
 

推荐答案

几分钟前,我还在解决同样的问题.我还在开发中,没有在生产中测试它,但我能够加载本地广告.对我来说,这很管用:

1. Make sure you added metadata APPLICATION_ID

    <manifest>
      <application>
      <!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->
      <meta-data
          android:name="com.google.android.gms.ads.APPLICATION_ID"
          android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
          ...

      </application>
    </manifest>

2. Make sure ads sdk is initialized before loading any ad

   MobileAds.initialize(context) {
        //Now you can load ad
        adLoader.loadAd(AdRequest.Builder().build())
    }

3. Use "/6499/example/native" adUnitId

现在我真的不能解释为什么,但为了调试,它与这个广告单元ID"/6499/example/native"一起工作.我在这example project中找到了它,它包含了如何加载和显示本地广告的工作实现.

Android相关问答推荐

无法安装后重新编译android代码'

了解数据加载在Kotlin中的工作原理

在Kotlin Jetpack Compose中点击按钮后启动另一个Android应用程序

list 合并失败,AGP 8.3.0

在命令行Android应用程序开发中苦苦挣扎

从未设置实时数据值

Android系统应用程序启用编程以太网网络共享

Android布局渲染问题

在Jetpack Compose中实现焦点突出的最佳方式?

具有数据库和升级潜力的移动应用程序开发(Android)供朋友使用

Jetpack Compose with Paging 3 发出太多网络请求

在模块 jetified-kotlin-stdlib-1.8.10 中发现重复的类 kotlin.random.jdk8,带有启动基准

如何将可重用的 ExtendedFloatingActionButton 与可重用的脚手架链接起来

在 Jetpack Compose 中使用 .observeAsState() 时,如何在更改 MutableLiveData 的值后开始执行一段代码?

如何在 Android Jetpack compose 中为列表初始填充设置动画

Jetpack compose 为网络检索视频帧导致延迟

多个项目 react-native android 构建错误

如何使伴奏导航 BottomSheet 完全展开?

未使用的内容填充参数

如何在 Kotlin 中使用反向绑定适配器将小写文本转换为大写?