Not able to use AdMob. Application doesn't start and stops unfortunately everytime

256 views
Skip to first unread message

Saurabh Srivastava

unread,
Jun 15, 2015, 4:46:30 AM6/15/15
to google-adm...@googlegroups.com


I have implemented the same example of Banner AdMob as given in the official site. Still the Application is not working.
Here is the code and the problem I'm facing.
Could anybody help me to fix this?

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <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:adUnitId="@string/banner_ad_unit_id"
        >
        </com.google.android.gms.ads.AdView>

</RelativeLayout>


--Java File--

package com.google.android.gms.example;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        AdView mAdView= (AdView) this.findViewById(R.id.adView);
        mAdView.setAdSize(AdSize.BANNER);
        AdRequest adRequest=new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}


--Mainifest File--

<?xml version="1.0" encoding="utf-8"?>
    package="com.google.android.gms.example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="18" />
    
    <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.example.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>
        <activity android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
    </application>

</manifest>


--strings.xml--

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">BannerExample</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>

</resources>

--XML design view--
adSize error doesn't go after adding adSize in XML file too..





--Import Export Jar files--





--Android Linking--




Can anybody tell we where am I going wrong.

Thank You,
Saurabh Srivastava

Vu Chau (MobileAds SDK Team)

unread,
Jun 15, 2015, 2:16:07 PM6/15/15
to google-adm...@googlegroups.com, saurabh.sri...@gmail.com
Hi Saurabh,

I ran your code (with the adSize set in code) and indeed observed the "Required XML attribute 'adSize' was missing" error.  At compile time, the IDE also complained that the ad size "must be set before loadAd is called."  However, if I rid of the line where the adSize is set in code, and define it instead in the XML, the app compiles and runs as expected.

Could you let us know if setting the adSize in the XML rectifies this issue?  If it does not, I would go ahead and double-check the library linking and jar imports, since the code we are using is already the barebone.  You might also consider migrating your project from Eclipse to Android Studio in order to receive full support.  Gradle will come in handy for future dependency management as the Android Quick Start demos.

Vu Chau
MobileAds SDK Team

Saurabh Srivastava

unread,
Jun 16, 2015, 4:55:31 AM6/16/15
to google-adm...@googlegroups.com, saurabh.sri...@gmail.com
Thank you Vu Chan, "Required XML attribute 'ádSize' missing" error has gone. But after cleaning and rebuilding, the app still stops unexpectedly. I don't know why the code is not working? 
How to run the app seamlessly?

Thanks,
Saurabh Srivastava 
Message has been deleted

Saurabh Srivastava

unread,
Jun 16, 2015, 4:59:58 AM6/16/15
to google-adm...@googlegroups.com
And as far as Android Studio is concerned, I have installed Android Studio.. But its a little bit confusing and .when I click "Run 'app' ", nothing pops up and I am unable to see whether something is running or not. 

Vu Chau (MobileAds SDK Team)

unread,
Jun 16, 2015, 4:21:49 PM6/16/15
to google-adm...@googlegroups.com, saurabh.sri...@gmail.com
Hi Saurabh,

I'm glad to hear the error has been resolved! Your implementation should be working seamlessly.  When you said the app still stops unexpectedly, are you experiencing crashes using our sample app?

In Android Studio, if you can share with us your stack trace (and code snippets if you are not running the sample app), that'd be great. We'll take a look and see what might be going wrong.
If you are looking to test the app in the emulator, you will have to set up an Android Virtual Device.  Make sure you have a MainActivity from which to launch the app, and check to see your Run Configuration has been properly set up for your project.  You may also check out our Getting Started guide for further assistance.

Let us know if you have any other questions!

Vu Chau
MobileAds SDK Team

Reply all
Reply to author
Forward
0 new messages