简体   繁体   English

Admob 6.4.1-膨胀类com.google.ads.AdView时出错

[英]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. 将admob添加到我的xml时,它会导致致命错误,从而导致应用程序崩溃。

I added the admob jar correctly in /libs and to build path. 我在/ libs中正确添加了admob jar并构建路径。

The xml looks like this xml看起来像这样

<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 这是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):无法初始化AdView:AdView已使用不是活动的上下文进行了初始化。 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):无法初始化AdView:AdView已使用不是活动的上下文进行了初始化。 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.ja 07-29 00:39:59.113:W / dalvikvm(8849):threadid = 1:线程以未捕获的异常退出(group = 0x40da9390)07-29 00:39:59.133:E / AndroidRuntime(8849):致命异常:主07-29 00:39:59.133:E / AndroidRuntime(8849):java.lang.RuntimeException:无法启动活动ComponentInfo {com.clickvote.app/com.clickvote.app.WebViewActivity}:android.view.InflateException:二进制XML文件第7行:膨胀类com.google.ads.AdView 07-29 00:39:59.133:E / AndroidRuntime(8849):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)07-29 00:39:59.133:E / AndroidRuntime(8849):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)07-29 00:39:59.133:E / AndroidRuntime(8849):在android.app.ActivityThread .access $ 600(ActivityThread.java:151)07-29 00:39:59.133:E / AndroidRuntime(8849):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1335)07-29 00:39: 59.133:E / AndroidRuntime(8849):位于android.os.Handler.dispatchMessage(Handler.ja va: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:3 va: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) :位于android.app.ActivityThread.main(ActivityThread.java:5511)07-29 00:39:59.133:E / AndroidRuntime(8849):位于java.lang.reflect.Method.invokeNative(本地方法)07-29 00 :39:59.133:E / AndroidRuntime(8849):在java.lang.reflect.Method.invoke(Method.java:511)07-29 00:39:59.133:E / AndroidRuntime(8849):在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):在dalvik.system.NativeStart.main(本机方法)07-29 00:39:59.133:E / AndroidRuntime(8849):由android。 view.InflateException:二进制XML文件第7行:膨胀类com.google.ads.AdView的错误07-29 00:39:59.133:E / AndroidRuntime(8849):位于android.view.LayoutInflater.createView(LayoutInflater.java: 613)07-29 00:3 9: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(Activit 9:59.133:E / AndroidRuntime(8849):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)07-29 00:39:59.133:E / AndroidRuntime(8849):在android.view.LayoutInflater.rInflate (LayoutInflater.java:746)07-29 00:39:59.133:E / AndroidRuntime(8849):在android.view.LayoutInflater.inflate(LayoutInflater.java:489)07-29 00:39:59.133:E / AndroidRuntime (8849):位于android.view.LayoutInflater.inflate(LayoutInflater.java:396)07-29 00:39:59.133:E / AndroidRuntime(8849):位于com.android.internal.app.ActionBarImpl.setCustomView(ActionBarImpl。 java:310)07-29 00:39:59.133:E / AndroidRuntime(8849):在com.clickvote.app.WebViewActivity.onCreate(WebViewActivity.java:45)07-29 00:39:59.133:E / AndroidRuntime( 8849):在android.app.Activity.performCreate(Activity.java:5066)07-29 00:39:59.133:E / AndroidRuntime(8849):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)07 -29 00:39:59.133:E / AndroidRuntime(8849):在android.app.ActivityThread.performLaunchActivity(Activit yThread.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. yThread.java:2311)07-29 00:39:59.133:E / AndroidRuntime(8849):... 11更多07-29 00:39:59.133:E / AndroidRuntime(8849):原因:java.lang。 Reflection.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更多07-29 00:39:59.133:E / AndroidRuntime(8849):原因:java.lang.RuntimeException:无法初始化AdView:AdView是使用不是活动的上下文初始化。 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):位于com.google.ads.AdView.a(SourceFile:402)07-29 00:39:59.133:E / AndroidRuntime(8849):位于com。 google.ads.AdView。(SourceFile:125)07-29 00:39:59.133:E / AndroidRuntime(8849):... 23更多07-29 00:39:59.133:E / AndroidRuntime(8849):由:com.google.ads.internal.b:AdView已使用不是活动的上下文进行了初始化。 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 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个

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. 好的,根据您的评论,我将发布较早的建议作为一些示例代码的答案,因为正如您所指出的那样,您对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. 无需将自定义视图膨胀委派给ActionBar ,而是自己对其进行膨胀,以确保使用了Activity上下文。 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. 如果在此之后您仍然遇到相同的问题,请向我推一下,我会做的更好。

尝试阅读您的错误:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM