Skip to content Skip to sidebar Skip to footer

Android: Problem With Google Admob Sample

I'm trying to create a sample admob application with just the bare essentials. I followed the directions and even copied most of the code from this site: http://code.google.com/mob

Solution 1:

The NPE happens because layout is null, i.e., there is no view with the ID R.layout.main.

Interpreting the original example

    // Lookup your LinearLayout assuming it’s been given
    // the attribute android:id="@+id/mainLayout"
    LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);

you need to add the attribute android:id to your LinearLayout (the ID should be different from R.id.main).

Solution 2:

Once you fix your NPE error you may get a further error.

You have to set a layouts height and width before adding it to a view.

AdViewadView=newAdView(this, AdSize.BANNER, "a14dc6ed8aead31");
adView .setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayoutlayout= (LinearLayout)findViewById(R.id.layoutMain);
layout.addView(adView);

Post a Comment for "Android: Problem With Google Admob Sample"