AdMob横幅广告未显示。错误代码2

时间:2018-12-16 18:59:58

标签: android admob

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

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

activity_test.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>

TestActivity.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())
    }
}

1 个答案:

答案 0 :(得分:0)

我忘了在网址中添加“ @ ”符号 ads:adUnitId="string/mainactivity_banner_ad_unit_id"位于activity_test.xml布局中

我很沮丧。这是正确的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>