Android Adview getting error

六月ゝ 毕业季﹏ 提交于 2019-12-25 19:45:32

问题


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>


来源:https://stackoverflow.com/questions/26341238/android-adview-getting-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!