Admob 6.4.1 - com.google.ads.AdView类错误

时间:2013-07-29 00:13:59

标签: java android eclipse sdk admob

将admob添加到我的xml时会导致致命错误,导致应用程序崩溃。

我在/ libs中正确添加了admob jar并构建了路径。

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>

这是logcat

  

07-29 00:39:59.113:E / Ads(8849):无法初始化AdView:AdView   用一个不是活动的上下文初始化。 07-29   00:39:59.113:E / Ads(8849):无法初始化AdView:AdView是   使用不是活动的上下文初始化。 07-29   00:39:59.113:W / dalvikvm(8849):threadid = 1:线程退出   未捕获的异常(组= 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:错误   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):引起:android.view.InflateException:   二进制XML文件行#7: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更多07-29   00:39:59.133:E / AndroidRuntime(8849):引起:   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多07-29 00:39:59.133:   E / AndroidRuntime(8849):引起:java.lang.RuntimeException:可以   没有初始化AdView:AdView是使用Context初始化的   不是活动。 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更多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更多

2 个答案:

答案 0 :(得分:1)

好的,根据您的评论,我将发布早期的建议作为一些示例代码的答案,因为正如您所指出的,您对Android很新。

您当前的代码:

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

基本上你应该尝试重写上面指出的那一行。而不是将自定义视图通胀委派给ActionBar,而是自己进行充气以确保使用Activity上下文。像这样:

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

那应该(可能)摆脱你目前看到的异常。我没有验证或尝试运行上面的代码段,所以要注意任何拼写错误。如果你在此之后仍然遇到同样的问题,请给我一个轻推,我会好好看看。

答案 1 :(得分:0)

尝试阅读错误:

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