简体   繁体   English

Android Studio-膨胀类com.google.ads.AdView时出错

[英]Android Studio - Error inflating class com.google.ads.AdView

I'm using new Android Studio. 我正在使用新的Android Studio。 I've done an application and works fine, now I've to add AdMob sdk. 我已经完成了一个应用程序,并且工作正常,现在我要添加AdMob sdk。 so I put the jar in 'libs' folder and right-click "add as library". 所以我将罐子放在“ libs”文件夹中,然后右键单击“添加为库”。 I run the project on my smartphone but the application crashes on startup. 我在智能手机上运行该项目,但是应用程序在启动时崩溃。

How can i solve this? 我该如何解决? I think it'll be something wrong with the import of AdMob sdk. 我认为导入AdMob sdk会出问题。

Thank you! 谢谢!

This is the xml 这是XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.application"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        //MY ACTIVITY
    </activity>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
            android:name="com.google.ads.AdActivity"
               android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

    <meta-data android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" android:value="true"/>
    <meta-data android:name="ADMOB_PUBLISHER_ID" android:value="a151964f48b17a7"/>

</application>

This is the layout xml 这是布局xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
            tools:context=".MainActivity">

<com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="MY_UNIT_ID"
        ads:loadAdOnCreate="true" >
</com.google.ads.AdView>

And this is the logcat 这是logcat

5-20 15:38:00.835  26121-26121/com.mnt.crudelibestmoments     E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mnt.crudelibestmoments/com.mnt.crudelibestmoments.MainActivity}: android.view.InflateException: Binary XML file line #195: Error inflating class com.google.ads.AdView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
    at android.app.ActivityThread.access$600(ActivityThread.java:127)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4448)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.view.InflateException: Binary XML file line #195: Error inflating class com.google.ads.AdView
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255)
    at android.app.Activity.setContentView(Activity.java:1835)
    at com.mnt.crudelibestmoments.MainActivity.onCreate(MainActivity.java:40)
    at android.app.Activity.performCreate(Activity.java:4465)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
    ... 11 more
    Caused by: java.lang.ClassNotFoundException: com.google.ads.AdView
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)

If i hide the AdMob code in layout xml, the application works properly 如果我将AdMob代码隐藏在布局xml中,则该应用程序可以正常运行

thank you! 谢谢!

it's a bit tricky but here is the solution: 这有点棘手,但这是解决方案:

The current preview release doesn't edit the build.gradle file, even if you have added the admob library to the dependencies in the menu. 即使已将admob库添加到菜单中的依赖项,当前预览版也不会编辑build.gradle文件。 What you can do is to add the dependency manually, it's very easy: 您可以做的是手动添加依赖项,这非常简单:

double click on the file "build.gradle" on the left in the navigation menu. 双击导航菜单左侧的文件“ build.gradle”。 Add the following line: 添加以下行:

compile files('libs/GoogleAdMobAdsSdk-6.4.1.jar') 编译文件(“ libs / GoogleAdMobAdsSdk-6.4.1.jar”)

It should look like: 它应该看起来像:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'

    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile files('libs/GoogleAdMobAdsSdk-6.4.1.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

Right click your project and go to Properties -> Java Build Path . 右键单击您的项目,然后转到Properties -> Java Build Path In Libraries add the AdMob SDK jar, and in Order and Export check the AdMob SDK library. Libraries添加AdMob SDK jar,然后在“ Order and Export检查AdMob SDK库。 Then clean and re-run your application. 然后清理并重新运行您的应用程序。

Previously, putting 3rd party libraries in the libs/ folder was sufficient for the Android build to bundle these libraries when compiling your app. 以前,将第3方库放在libs /文件夹中足以使Android构建在编译应用程序时捆绑这些库。 But since the release of v22 of the Android tools, you now have to explicitly add the library to your build path and export it. 但是自从Android工具v22发行以来,您现在必须将库明确添加到构建路径并导出。

I was the same and achieve fix, I have done just the same as you and I miss you just do the following: 我是一样的并且可以解决问题,我所做的和您一样,而我想念您,请执行以下操作:

open a terminal (windows or linux) and go to the root of your project, eg AndroidStudioProjects / MyProject 打开终端(Windows或Linux)并转到项目的根目录,例如AndroidStudioProjects / MyProject

Run the command: 运行命令:

Linux(is the file gradlew): ./gradlew clean Linux(是gradlew文件):./gradlew clean

Windows(is the file gradlew.bat): gradlew clean Windows(是gradlew.bat文件):gradlew clean

Now goto: Build > Rebuild Project 现在转到:构建>重建项目

That's it, enjoy!!! 就是这样,享受!!!

Sorry for the bad English 对不起英语不好

I got same problem.I changed Android.jar to 4.2.2. 我遇到了同样的问题,我将Android.jar更改为4.2.2。 Now it works fine in both emulator and phone 现在,它可以在模拟器和手机上正常工作

This may help you integrate admob with your application but google is not going to accept any updated or new apps which use this type of admob integration following 1 August 2014. 这可能会帮助您将admob与您的应用程序集成在一起,但2014年8月1日之后,谷歌将不接受使用此类admob集成的任何更新或新应用程序。

https://developers.google.com/mobile-ads-sdk/download#downloadandroid https://developers.google.com/mobile-ads-sdk/download#downloadandroid

Instead I recommend you to use Google Play Services version as suggested in above document. 相反,我建议您按照上述文档中的建议使用Google Play服务版本。

You are most likely using a AdView layout with package name com.google.ads.AdView. 您最有可能使用软件包名称为com.google.ads.AdView的AdView布局。 Change it to: 更改为:

 <com.google.android.gms.ads.AdView
        xmlns:googleads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@+id/ad"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        />

I am also using Android Studio, with build.gradle dependency set like this: 我也在使用带有以下build.gradle依赖项设置的Android Studio:

dependencies {
compile 'com.android.support:appcompat-v7:+'
compile 'com.google.android.gms:play-services:4.3.23'
}

Moving forward,we are not required to manually add libs that already have Maven coordinates to point to, so do NOT do this. 展望未来,我们不需要手动添加已经有Maven坐标指向的库,所以不要这样做。

dependencies {
compile files('libs/android-support-v4.jar')
compile files('libs/GoogleAdMobAdsSdk-6.4.1.jar')
}

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

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