T.dog T.dog - 1 month ago 12
Android Question

AdMob banner issues android

I'd like to place ads in my MainActivity below FrameLayout like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
xmlns:ads="http://schemas.android.com/apk/res-auto">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:id="@+id/fragmentContainer"
android:layout_centerHorizontal="true">
</FrameLayout>

<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>




I have added this to my Manifest:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>


And my build.gradle also looks fine. Here is my java code:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

MobileAds.initialize(getApplicationContext(),"ca-app-pub-xxxxxxxx~xxx");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // An example device ID
.build();
mAdView.loadAd(request);


But when I run the app I can't see the ads and in the LogCat I got this errors:

W/Ads: Invalid unknown request error: [Cannot determine request type. Is your ad unit id correct?]


W/Ads: There was a problem getting an ad response. ErrorCode: 1


W/Ads: Failed to load ad: 1


I have tried almost everything I found on the web but nothing worked for me. I think maybe the issue is in my activity_main.xml because of the FrameLayout doesn't give enough place to the ads.

Answer

Your code seems fine (although you should call MobileAds.initialize() from the onCreate() method of your Application class, not from an Activity).

Looks like the only problem could be the banner_ad_unit_id resource being an invalid ad unit ID (as the error message also suggests).