Can't get AdMob to work (using Android, LibGDX)

I've done a lot of research, but not much of it makes sense since I'm not familiar with placing in-app ads (this is my first time making an app, and my first time working with ads).

Here's my updated code:


import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;


public class MainActivity extends AndroidApplication {
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.main); //Changed it back to main, got rid of an error

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useGL20 = true;

        initialize(new MyGame(), cfg);

        AdView adView;
        adView = new AdView(this);
        adView.setAdUnitId("My Ad Unit ID (just using this string for posting purposes");

        LinearLayout layout = (LinearLayout) findViewById(; //Changed to After looking at my class, I saw that there was no id.linearlayout, so I changed it to, which did exist.
        layout.addView(adView); //This is line 44, where the NullPointer is caused

        AdRequest adRequest = new AdRequest.Builder().build();


Here's the updated AndroidManifest XML code:

   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="5" android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

        android:label="@string/app_name" >

        <meta-data android:name=""
           android:value="@integer/google_play_services_version" />

            android:label="Flappy Nerd"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

         <activity android:name=""



Here's the main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >


Here's the logcat:

03-16 16:46:50.126: E/AndroidRuntime(10338): FATAL EXCEPTION: main
03-16 16:46:50.126: E/AndroidRuntime(10338): java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.NullPointerException
03-16 16:46:50.126: E/AndroidRuntime(10338):    at
03-16 16:46:50.126: E/AndroidRuntime(10338):    at
03-16 16:46:50.126: E/AndroidRuntime(10338):    at$700(
03-16 16:46:50.126: E/AndroidRuntime(10338):    at$H.handleMessage(
03-16 16:46:50.126: E/AndroidRuntime(10338):    at android.os.Handler.dispatchMessage(
03-16 16:46:50.126: E/AndroidRuntime(10338):    at android.os.Looper.loop(
03-16 16:46:50.126: E/AndroidRuntime(10338):    at
03-16 16:46:50.126: E/AndroidRuntime(10338):    at java.lang.reflect.Method.invokeNative(Native Method)
03-16 16:46:50.126: E/AndroidRuntime(10338):    at java.lang.reflect.Method.invoke(
03-16 16:46:50.126: E/AndroidRuntime(10338):    at$
03-16 16:46:50.126: E/AndroidRuntime(10338):    at
03-16 16:46:50.126: E/AndroidRuntime(10338):    at dalvik.system.NativeStart.main(Native Method)
03-16 16:46:50.126: E/AndroidRuntime(10338): Caused by: java.lang.NullPointerException
03-16 16:46:50.126: E/AndroidRuntime(10338):    at
03-16 16:46:50.126: E/AndroidRuntime(10338):    at
03-16 16:46:50.126: E/AndroidRuntime(10338):    at
03-16 16:46:50.126: E/AndroidRuntime(10338):    at

Here is an updated LogCat: When I take out the line of code that seemed to be causing NullPointers "layout.addView(adView);", the app runs fine, but I get these messages from LogCat:

03-16 17:04:06.723: E/GooglePlayServicesUtil(11199): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.

03-16 17:04:09.265: I/Ads(11199): Ad finished loading.

03-16 17:05:09.269: I/Ads(11199): Ad is not visible. Not refreshing ad.


You seem to have missed the activity part for ads in your Manifest file:

   android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

You'll have to adapt all your routes to this. If you can compile but it doesn't work, you're probably using a deprecated version of AdMob. You'll have to import the google-play_lib external library into your project, here's a link on how to do it.

Your code is such a mess!

There are several errors in it you should fix. To avoid errors your setContentView(R.layout.main); must be the first line right after the super.onCreate(savedInstanceState);

And the most confusing one, to me, is:

Why do you have declared a RelativeLayout inside the App's Manifest?? It must be declared inside main.xml. This is the cause of your NullPointerException. It happens because can't find nothing with this ID inside your layout, thus making layout variable null and when called causes a crash (cus it is wrongly declared in your manifest).

As @yugidroid, your code is pretty messy! :P But you will get it eventually, don't worry!

Most important thing, don't put layouts on your manifest, they don't go there! In your folder /layout (inside your project) create a new xml file called "activity_main.xml" and fill it with this:

 <RelativeLayout xmlns:android=""

On your java file, change to

adView.setAdUnitId("xx-xxx-xxx-1234567890123456/1234567890"); //Change the numbers to what google sent you in the email!!!

and Adrequest change to

AdRequest adRequest = new AdRequest.Builder().build();

(not recommended for testing but for now it will do)

I recommend you though to read the documentation again though, it is pretty straight forward

I bet you are getting the NullPointerException on


It is almost certainly to do with the #initialize method calling #setContentView and replacing R.layout.main with something else.

Need Your Help

SQL Server 2008 Open Master Key error upon physical server change over

sql sql-server-2008 encryption aes

I copied a SQL Server database from one system to the next, identical setup, but completely different physical machine. I used Norton Ghost and recoverd files manually, for example, the entire SQL ...

Dynamically Change Content by clicking RibbonPane using Prism

wpf prism ribbon

I'm using Microsoft Ribbon and Prism to develop my application. I have in my main window 2 regions: one for the ribbon and the other to inject a view depending on the button clicked in my ribbon.