我在将广告纳入我的应用时遇到了一些问题。我收到以下错误:构造函数AdRequest()。在线:adView.loadAd(new AdRequest());
以下是我正在使用的包含广告视图的xml文件:
<?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>
这是我的android清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blueshift"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<activity
android:name="com.example.blueshift.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我用来尝试加载包含错误的添加的java代码:
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
感谢您的帮助。
更新:
遵循Kishan的建议并实施了他的更改,但现在我收到错误方法loadAd(AdRequest)未定义AdView.loadAd(adRequest)行上的AdView类型;有什么建议吗?
答案 0 :(得分:0)
您尚未添加Adrequest,因此您无法使用此功能:
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);
有关加载广告的详细信息,请使用此Link.
答案 1 :(得分:0)
关于错误消息:
对于AdView类型,方法loadAd(AdRequest)未定义 line adView.loadAd(adRequest);
问题似乎是您设置了错误的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,而您已经以编程方式加载广告。
有关更多信息,请参阅本指南:https://developers.google.com/mobile-ads-sdk/docs/admob/android/banner