0

我的应用程序完全停止显示横幅广告。调试后,我发现我收到了 Ad Mob 错误代码 2(等待广告响应超时),即使是测试广告单元 ID

问题是我的互联网连接良好,实际上是 Intersti

活动测试.xml

<android.support.constraint.ConstraintLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   xmlns:ads="http://schemas.android.com/apk/res-auto"
   tools:context=".Activities.TestActivity">

   <com.google.android.gms.ads.AdView
    android:id="@+id/adViewTest"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="visible"
    ads:adSize="BANNER"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    ads:adUnitId="string/mainactivity_banner_ad_unit_id"
    app:layout_constraintBottom_toBottomOf="parent"
    />
</android.support.constraint.ConstraintLayout>

测试活动.kt

class TestActivity : AppCompatActivity() {
    val TAG = "TestActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
        val mAdView = findViewById<AdView>(R.id.adViewTest)
        mAdView.adListener = object : AdListener() {
            override fun onAdFailedToLoad(errorCode: Int) {
                super.onAdFailedToLoad(errorCode)
                Log.e(TAG, "onAdFailedToLoad BannerAd - ErrorCode = $errorCode")
            }

            override fun onAdClosed() {
                super.onAdClosed()
                Log.e(TAG, "onAdClosed BannerAd")
            }
            override fun onAdLoaded() {
                super.onAdLoaded()
                Log.e(TAG, "onAdLoaded BannerAd")
            }
        }
        mAdView.loadAd(AdRequest.Builder().build())
    }
}
4

1 回答 1

0

我忘了在 activity_test.xml 布局中包含“ @ ”符号 ads:adUnitId="string/mainactivity_banner_ad_unit_id"

我很沮丧。这是正确的 XML 布局代码

<android.support.constraint.ConstraintLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   xmlns:ads="http://schemas.android.com/apk/res-auto"
   tools:context=".Activities.TestActivity">

   <com.google.android.gms.ads.AdView
    android:id="@+id/adViewTest"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="visible"
    ads:adSize="BANNER"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    ads:adUnitId="@string/mainactivity_banner_ad_unit_id" 
    app:layout_constraintBottom_toBottomOf="parent"
    />
</android.support.constraint.ConstraintLayout>
于 2018-12-16T18:59:58.480 回答