I'm having some trouble incorporating ads to my app. I'm getting the following error: The constructor AdRequest(). On the line: adView.loadAd(new AdRequest());
Here is the xml file I'm using containing the ad view:
<?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>
Here is my android manifest:
<?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>
and here is the java code I'm using to try and load the add containing the error:
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
Thanks for any help.
UPDATE:
Followed Kishan's advice and implemented his changes, however now I'm getting the error The method loadAd(AdRequest) is undefined for the type AdView on the line adView.loadAd(adRequest); Any advice?
You havent add Adrequest so you are no getting use this:
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);
For further details about load ad use this Link.
About the error msg:
The method loadAd(AdRequest) is undefined for the type AdView on the line adView.loadAd(adRequest);
The problems seems to be that you have set the wrong xml namespace:
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
this refers to the legacy Admob and you want to use the new admob, so use instead:
xmlns:ads="http://schemas.android.com/apk/res-auto"
You may also check the import of AdView in your Activity code, it needs to be the following:
import com.google.android.gms.ads.AdView
Also remove this attribute from the AdView in the xml:
ads:loadAdOnCreate="true"
because that was only available for legacy Admob and you are already loading the ad programmatically.
For more infos about see this guide: https://developers.google.com/mobile-ads-sdk/docs/admob/android/banner
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.