Android Adview getting error

2019-09-25 09:06发布

问题:

I get these errors when placing adview ad.

  1. error: Error parsing XML: unbound prefix <com.google.android.gms.ads.AdView
    android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="wrap_content" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-2852987101120590/2338373260" />

  2. error: adView cannot be resolved or is not a field AdView adView = (AdView)this.findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); adView.loadAd(adRequest);

where is problem? please help. i get those two errors.

回答1:

Add this line in xml file

xmlns:ads="http://schemas.android.com/apk/res-auto"

So, example

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/ad_unit_id"/>
</RelativeLayout>