简体   繁体   English

com.google.ads.AdView类错误

[英]Error inflating class com.google.ads.AdView

Hello I am new to android dev and admob. 你好我是android dev和admob的新手。 I am trying to make a simple test app with a button, a couple pages and admob at the bottom. 我正在尝试使用按钮,几页和底部的admob制作一个简单的测试应用程序。 Everything works fine until I try implementing admob. 一切正常,直到我尝试实现admob。 Here is my code: 这是我的代码:

HelloAndroid.java: HelloAndroid.java:

package com.manny.HelloAndroid;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    private static final int ACTIVITY_CREATE = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.main);
        ImageView image = (ImageView) findViewById(R.id.test_image);
        //image.setScaleType(ImageView.ScaleType.FIT_XY);

    }
    public void onClick(View v){
        Intent i = new Intent(this, HelloPage2.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }
}

main.xml: main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView 
        android:id="@+id/test_image"
        android:src="@drawable/bak_vert_480x800"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:scaleType="fitXY"
        />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <ImageButton 
        android:id="@+id/test_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/btn_arrow"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="200dp"
        android:onClick="onClick"
        />
     <com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="b14be806e9ddf1f"
                         ads:adSize="BANNER"
                         ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
                         ads:loadAdOnCreate="true"/>

</RelativeLayout>

hello android Manifest: 你好android清单:

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

    <uses-sdk android:minSdkVersion="7" 
        android:targetSdkVersion="13"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <activity
            android:name=".HelloAndroid"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".HelloPage2">
        </activity>
    <activity android:name="com.google.ads.AdActivity"
                    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|s    mallestScreenSize"/>
  </application>
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>

logcat output: logcat输出:

05-09 13:12:00.859: D/dalvikvm(24278): GC_EXTERNAL_ALLOC freed 51K, 44% free 3054K/5379K, external 2357K/2773K, paused 94ms
05-09 13:12:00.937: D/dalvikvm(24278): GC_EXTERNAL_ALLOC freed 1K, 44% free 3053K/5379K, external 3857K/4816K, paused 31ms
05-09 13:12:01.085: D/AndroidRuntime(24278): Shutting down VM
05-09 13:12:01.085: W/dalvikvm(24278): threadid=1: thread exiting with uncaught exception (group=0x4001e560)
05-09 13:12:01.109: E/AndroidRuntime(24278): FATAL EXCEPTION: main
05-09 13:12:01.109: E/AndroidRuntime(24278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.manny.HelloAndroid/com.manny.HelloAndroid.HelloAndroid}: android.view.InflateException: Binary XML file line #30: Error inflating class com.google.ads.AdView
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1702)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1722)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.app.ActivityThread.access$1500(ActivityThread.java:124)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:974)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.os.Looper.loop(Looper.java:130)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.app.ActivityThread.main(ActivityThread.java:3821)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at java.lang.reflect.Method.invokeNative(Native Method)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at java.lang.reflect.Method.invoke(Method.java:507)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at dalvik.system.NativeStart.main(Native Method)
05-09 13:12:01.109: E/AndroidRuntime(24278): Caused by: android.view.InflateException: Binary XML file line #30: Error inflating class com.google.ads.AdView
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:253)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.app.Activity.setContentView(Activity.java:1716)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at com.manny.HelloAndroid.HelloAndroid.onCreate(HelloAndroid.java:17)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1666)
05-09 13:12:01.109: E/AndroidRuntime(24278):    ... 11 more
05-09 13:12:01.109: E/AndroidRuntime(24278): Caused by: java.lang.ClassNotFoundException: com.google.ads.AdView in loader dalvik.system.PathClassLoader[/data/app/com.manny.HelloAndroid-2.apk]
05-09 13:12:01.109: E/AndroidRuntime(24278):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.view.LayoutInflater.createView(LayoutInflater.java:471)
05-09 13:12:01.109: E/AndroidRuntime(24278):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
05-09 13:12:01.109: E/AndroidRuntime(24278):    ... 20 more
05-09 13:12:11.593: I/Process(24278): Sending signal. PID: 24278 SIG: 9

I am using GoogleAdMobAdsSdkAndroid-6.0.0. 我使用的是GoogleAdMobAdsSdkAndroid-6.0.0。 Again I am very new to this and I have tried using examples off multiple sites but the app continues to crash when it hits a page with admob in it. 我再次对此非常陌生,我尝试在多个站点上使用示例,但当应用程序遇到带有admob的页面时,应用程序继续崩溃。 If someone can help me it would be much appreciated. 如果有人可以帮助我,我将不胜感激。 Thank you. 谢谢。 If more code is needed let me know. 如果需要更多代码,请告诉我。

Have you recently updated your Android Tools to r17 or higher? 您最近是否将Android工具更新为r17或更高版本? If so, you need to create a libs/ folder in your project, and physically put the SDK in the project. 如果是这样,您需要在项目中创建一个libs /文件夹,并将SDK物理地放入项目中。 Or, in the Java Build Path, you must Order and Export the SDK jar. 或者,在Java Build Path中,您必须订购和导出SDK jar。

Otherwise, the jar doesn't get included when your apk gets compiled, and all hell breaks loose when you run AdMob code at runtime. 否则,当您的apk被编译时,jar不会被包含在内,并且当您在运行时运行AdMob代码时,所有地狱都会破坏。

NOTE: This applies to any library your project references. 注意:这适用于项目引用的任何库。

For me it worked like this. 对我而言,它就是这样的。

  • close eclipse 近食
  • add manually the sdk ad jar file in your project's "libs" folder 在项目的“libs”文件夹中手动添加sdk ad jar文件
  • open eclipse 开日食
  • refresh your project till the sdk jar appears on libs folder 刷新您的项目,直到sdk jar出现在libs文件夹中
  • go to your project's properties > Java Build Path > Library Tab and choose "add Jars" instead of an external one. 转到项目的属性> Java Build Path> Library选项卡,然后选择“添加Jars”而不是外部。 and select the sdk from the list below. 并从下面的列表中选择sdk。
  • go to "Order and Export" tab and select again the SDK 转到“订购和导出”选项卡,然后再次选择SDK
  • click ok AND ITS DONE. 点击确定并完成。

Then just add the proper code in the manifest and the xml. 然后只需在清单和xml中添加正确的代码即可。

I was experiencing this, too. 我也经历过这个。 The build path didn't seem to work no matter what I did. 无论我做什么,构建路径似乎都不起作用。 So I exited Eclipse, created a new libs folder in the workbench package directory. 所以我退出Eclipse,在workbench包目录中创建了一个新的libs文件夹。 Added the GoogleAdMobAdsSdk-6.0.1.jar to that folder. 将GoogleAdMobAdsSdk-6.0.1.jar添加到该文件夹​​。 I DID NOT add the whole SDK, just the jar file. 我没有添加整个SDK,只是jar文件。 Then restarted Eclipse. 然后重启Eclipse。 I had to refresh the project inspector column to show the changes I made outside of Eclipse. 我不得不刷新项目检查器列以显示我在Eclipse之外所做的更改。 It showed the new libs folder and jar file, but it still didn't work... Then I right clicked on the new libs folder and set it as a SOURCE FOLDER. 它显示了新的libs文件夹和jar文件,但它仍然无效......然后我右键单击新的libs文件夹并将其设置为SOURCE FOLDER。 NOW EVERYTHING WORKS! 现在一切正常!

Im using Eclipse Version: Indigo Service Release 2 Build id: 20120216-1857 我正在使用Eclipse版本:Indigo Service Release 2 Build id:20120216-1857

Android Development Tools 18.0.0.v201203301601-306762 Android开发工具18.0.0.v201203301601-306762

It seems that maybe the tools are ahead of the documentation... or some kind of Eclipse bug.... Very aggravating! 似乎这些工具可能超出了文档......或者某种Eclipse bug ......非常恶化!

I had the same problem and noticed that ordering the export list is also important. 我有同样的问题,并注意到订购导出列表也很重要。
This is mine, this way it works: 这是我的,这种方式有效:

在此输入图像描述

What I did - is working for me now - API Level 17 For admob: 我做了什么 - 现在为我工作 - API Level 17对于admob:
- Add adview code to your Layout with appropriate ID - 使用适当的ID向您的版面添加广告代码
- Add Admob manifest code to your project manifest file - 将Admob清单代码添加到项目清单文件中
- Add an external GoogleAdmobAdsSDK....jar Jar file to your Library (in Properties\\Java Build path) - 将外部GoogleAdmobAdsSDK .... jar Jar文件添加到您的库中(在Properties \\ Java Build路径中)
- Check to choose GoogleAdmobAdsSDK....jar in tab Order and Export - 选中“选项和导出”选项卡中的GoogleAdmobAdsSDK .... jar
- Restart your ADT - 重新启动ADT

DEGUB = WORKING NOW. DEGUB =现在正在工作。

In Android Studio: 1. Paste the Google AdMob SDK into the libs folder for your project 2. Add an entry to dependencies section of your build.gradle for the project pointing at the jar. 在Android Studio中:1。将Google AdMob SDK粘贴到项目的libs文件夹中2.为build.gradle的依赖项部分添加一个条目,指向指向jar的项目。 3. Rebuild your project 3.重建您的项目

dependencies {
...
compile files('libs/GoogleAdMobAdsSdk-6.4.1.jar', '... other entries...')
}

Note that compile files takes multiple jar entries. 请注意,编译文件需要多个jar条目。 If you already have a compile files entry you can just add to it by separating entries with a comma. 如果您已经有一个编译文件条目,您可以通过用逗号分隔条目来添加它。

同样的错误出现在我身上,我用几个简单的步骤解决你需要从你的项目中删除appcombat依赖项,以删除右键单击你的项目 - > android->删除app的战斗,并确保你已选择android 4.2.2然后导入google admob jar进入你的项目。

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

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