二进制XML文件行#6:com.google.ads.AdView类错误

时间:2014-07-01 19:49:59

标签: android admob

我的logcat在向我的项目添加admob时出现了这样的错误。我尝试手动添加sdk jar文件但仍然无效。我的libs文件夹包含jars-google-play-services和   android-support-v4.i已经引用其他帖子有相同的错误,但没有任何对我有用。

logcat的

07-02 00:26:42.082: E/AndroidRuntime(26811): FATAL EXCEPTION: main
07-02 00:26:42.082: E/AndroidRuntime(26811): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xmlpullparserproject/com.example.xmlpullparserproject.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class com.google.ads.AdView
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.os.Looper.loop(Looper.java:153)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.app.ActivityThread.main(ActivityThread.java:5086)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at java.lang.reflect.Method.invokeNative(Native Method)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at java.lang.reflect.Method.invoke(Method.java:511)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at dalvik.system.NativeStart.main(Native Method)
07-02 00:26:42.082: E/AndroidRuntime(26811): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.google.ads.AdView
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:258)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.app.Activity.setContentView(Activity.java:1867)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at com.example.xmlpullparserproject.MainActivity.onCreate(MainActivity.java:31)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.app.Activity.performCreate(Activity.java:5020)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-02 00:26:42.082: E/AndroidRuntime(26811):    ... 11 more
07-02 00:26:42.082: E/AndroidRuntime(26811): Caused by: java.lang.ClassNotFoundException: com.google.ads.AdView
07-02 00:26:42.082: E/AndroidRuntime(26811):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
07-02 00:26:42.082: E/AndroidRuntime(26811):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
07-02 00:26:42.082: E/AndroidRuntime(26811):    ... 21 more

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="xyz"
                         ads:adSize="BANNER"

                         />
    <ListView
        android:id="@+id/lv_countries"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        tools:context=".MainActivity" />

</RelativeLayout>

MainActvity

setContentView(R.layout.activity_main);

        AdView adView = (AdView)this.findViewById(R.id.adView);

        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)       // 
        .addTestDevice("3a2d1115ab64e3") // 
         .build() ;
        adView.loadAd(adRequest);

2 个答案:

答案 0 :(得分:2)

ad xml命名空间错误:

在GoogleAdMobAdsSdk-6.4.1.jar或更早版本中,您使用:

<com.google.ads.AdView 

使用命名空间:

 xmlns:ads= "http://schemas.android.com/apk/lib/com.google.ads"

通过播放服务库在Google admob中,您可以使用:

<com.google.android.gms.ads.AdView

使用命名空间:

 xmlns:ads="http://schemas.android.com/apk/res-auto

答案 1 :(得分:1)

尝试清理和重建项目。但错误很明显,它无法找到google play sdk jar中的com.google.ads.AdView类。如果您已更新到工具api 17+,则需要物理创建一个libs文件夹并将该jar放在该文件夹中,或在首选项中设置导出路径。

<com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="xyz"
                         ads:adSize="BANNER"

                         />

我还注意到您的adview调用看起来与新版本不同:(缺少.gms)

  <com.google.android.gms.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="MY_AD_UNIT_ID"
                         ads:adSize="BANNER"/>