Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Admob crashes application

This question was added here http://stackoverflow.com/questions/17914355/admob-6-4-1-error-inflating-class-com-google-ads-adview but still having the issue.

When adding admob to my xml it causes a fatal error which leads to crash the application.

I added the admob jar correctly in /libs and to build path.

The xml looks like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right" >

    <com.google.ads.AdView 
                        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
                         android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="#######"
                         ads:adSize="BANNER"
                         ads:testDevices="######"
                         ads:loadAdOnCreate="true"/> </RelativeLayout>

The Activity code ( I actually dont put any admob code here, since admob runs from the xml, but still getting the error)

public class WebViewActivity extends DroidGap { public static String LOG_TAG = "name"; private Dialog mLoadingDialog;

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setCustomView(R.layout.ab); // <-- !!
    getActionBar().setDisplayShowCustomEnabled(true);
    getActionBar().setDisplayShowHomeEnabled(false);
}

}

Here is logcat

07-29 00:39:59.113: E/Ads(8849): Could not initialize AdView: AdView was initialized with a Context that wasn't an Activity. 07-29 00:39:59.113: E/Ads(8849): Could not initialize AdView: AdView was initialized with a Context that wasn't an Activity. 07-29 00:39:59.113: W/dalvikvm(8849): threadid=1: thread exiting with uncaught exception (group=0x40da9390) 07-29 00:39:59.133: E/AndroidRuntime(8849): FATAL EXCEPTION: main 07-29 00:39:59.133: E/AndroidRuntime(8849): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.clickvote.app/com.clickvote.app.WebViewActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.google.ads.AdView 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.ActivityThread.access$600(ActivityThread.java:151) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.os.Handler.dispatchMessage(Handler.java:99) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.os.Looper.loop(Looper.java:155) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.ActivityThread.main(ActivityThread.java:5511) 07-29 00:39:59.133: E/AndroidRuntime(8849): at java.lang.reflect.Method.invokeNative(Native Method) 07-29 00:39:59.133: E/AndroidRuntime(8849): at java.lang.reflect.Method.invoke(Method.java:511) 07-29 00:39:59.133: E/AndroidRuntime(8849): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 07-29 00:39:59.133: E/AndroidRuntime(8849): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796) 07-29 00:39:59.133: E/AndroidRuntime(8849): at dalvik.system.NativeStart.main(Native Method) 07-29 00:39:59.133: E/AndroidRuntime(8849): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.google.ads.AdView 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.view.LayoutInflater.createView(LayoutInflater.java:613) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 07-29 00:39:59.133: E/AndroidRuntime(8849): at com.android.internal.app.ActionBarImpl.setCustomView(ActionBarImpl.java:310) 07-29 00:39:59.133: E/AndroidRuntime(8849): at com.clickvote.app.WebViewActivity.onCreate(WebViewActivity.java:45) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.Activity.performCreate(Activity.java:5066) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311) 07-29 00:39:59.133: E/AndroidRuntime(8849): ... 11 more 07-29 00:39:59.133: E/AndroidRuntime(8849): Caused by: java.lang.reflect.InvocationTargetException 07-29 00:39:59.133: E/AndroidRuntime(8849): at java.lang.reflect.Constructor.constructNative(Native Method) 07-29 00:39:59.133: E/AndroidRuntime(8849): at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.view.LayoutInflater.createView(LayoutInflater.java:587) 07-29 00:39:59.133: E/AndroidRuntime(8849): ... 20 more 07-29 00:39:59.133: E/AndroidRuntime(8849): Caused by: java.lang.RuntimeException: Could not initialize AdView: AdView was initialized with a Context that wasn't an Activity. 07-29 00:39:59.133: E/AndroidRuntime(8849): at com.google.ads.AdView.a(SourceFile:402) 07-29 00:39:59.133: E/AndroidRuntime(8849): at com.google.ads.AdView.<init>(SourceFile:125) 07-29 00:39:59.133: E/AndroidRuntime(8849): ... 23 more 07-29 00:39:59.133: E/AndroidRuntime(8849): Caused by: com.google.ads.internal.b: AdView was initialized with a Context that wasn't an Activity. 07-29 00:39:59.133: E/AndroidRuntime(8849): at com.google.ads.AdView.a(SourceFile:389) 07-29 00:39:59.133: E/AndroidRuntime(8849): ... 24 more

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Looks like you're getting some good help over in the StackOverflow question. :thumbsup: That's probably your best bet for this kind of question, but I'm glad you posted here, too. I've never come across this error in my limited use of AdMob, so I'm afraid I can't be of much help. Your problem might be related to PhoneGap as well (since you are extending the DroidGap class). DroidGap does inherit from Activity, but maybe the context is getting changed somewhere in that chain. Could you try getting it to work in a simple Hello World Android project using a regular Android Activity? Then you could compare a working example with your broken one.

HI Ben .. can you give some example code to place admob code on the blog Reader android aps

Thank you

Ben Jakuben
Ben Jakuben
Treehouse Teacher

I'll write this down as an idea for a Quick Tip! In the mean time, check out a tutorial online by searching for something like "android admob code example". Here's one that covers the basics: http://androidcodexamples.blogspot.com/2013/02/integrating-google-ads-from-admob-in.html