D.M
D.M

Reputation: 11

Admob 6.4.1 - Error inflating class com.google.ads.AdView

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>

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.(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

Upvotes: 0

Views: 1275

Answers (2)

MH.
MH.

Reputation: 45493

Okay, based on your comment I'm going to post the earlier suggestion as an answer with some sample code, since, as you pointed out, you're quite new to Android.

Your current code:

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);
    }
}

Basically you should try to rewrite the line indicated above. Rather than delegating the custom view inflation to the ActionBar, inflate it yourself to ensure that an Activity context is used. Like so:

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

    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View customActionBarView = getLayoutInflater().inflate(R.layout.ab, null, false);
        getActionBar().setCustomView(customActionBarView);
        getActionBar().setDisplayShowCustomEnabled(true);
        getActionBar().setDisplayShowHomeEnabled(false);
    }
}

That should (probably) get rid of the exception you're currently seeing. I didn't validate or try to run the snippet above, so be aware of any typos. If you're still experiencing the same problem after this, give me a nudge and I'll take a better look.

Upvotes: 1

jtt
jtt

Reputation: 13541

Try reading your error:

 AdView was initialized with a Context that wasn't an Activity

Upvotes: 0

Related Questions