Java Android-使用admob和adRquest加载广告时出现问题
我在将广告合并到我的应用程序时遇到一些问题。我得到以下错误:构造函数请求。在线:adView.loadadnewaddrequest 下面是我使用的包含广告视图的xml文件:Java Android-使用admob和adRquest加载广告时出现问题,java,android,xml,admob,adview,Java,Android,Xml,Admob,Adview,我在将广告合并到我的应用程序时遇到一些问题。我得到以下错误:构造函数请求。在线:adView.loadadnewaddrequest 下面是我使用的包含广告视图的xml文件: <?xml version="1.0"?> <RelativeLayout tools:ignore="MergeRootFrame" tools:context="com.example.blueshift.MainActivity" android:layout_height="
<?xml version="1.0"?>
<RelativeLayout tools:ignore="MergeRootFrame"
tools:context="com.example.blueshift.MainActivity"
android:layout_height="match_parent"
android:layout_width="match_parent" android:id="@+id/container"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
<com.example.blueshift.CustomView android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/custom_view"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/textView1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="djknfjndj" android:layout_marginBottom="128dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/textView2"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="djknfjndj" android:layout_marginBottom="160dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/button1"
android:text="Play Again"
android:layout_marginBottom="114dp"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:layout_above="@+id/textView1"/>
<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="id"
ads:loadAdsOnCreate="true"/>
</RelativeLayout>
谢谢你的帮助
更新:
遵循Kishan的建议并实现了他的更改,但是现在我得到了一个错误:在AdView.loadAdAdRequest行中,类型AdView的loadAdAdRequest方法未定义;有什么建议吗?您没有添加地址请求,因此无法使用此选项:
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
有关加载广告的更多详细信息,请使用以下关于错误消息的信息:
上的类型AdView的loadAdAdRequest方法未定义
line adView.loadAdadRequest
问题似乎是您设置了错误的xml命名空间:
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
这是指旧的Admob,您希望使用新的Admob,因此请改用:
xmlns:ads="http://schemas.android.com/apk/res-auto"
您也可以在活动代码中检查AdView的导入,它需要如下所示:
import com.google.android.gms.ads.AdView
还要从xml中的AdView中删除此属性:
ads:loadAdOnCreate="true"
因为这仅适用于旧版Admob,并且您已经在以编程方式加载该广告
有关详细信息,请参阅本指南:感谢您的建议,但现在出现以下错误:类型AdView的loadAdAdRequest方法未定义
import com.google.android.gms.ads.AdView
ads:loadAdOnCreate="true"