Android无法实例化以下类: - com.google.android.gms.ads.AdView(Open Class,Show Exception)

时间:2014-08-08 15:57:50

标签: java android xml google-play-services

我试图在我的应用上投放广告,但是当我将广告放在xml上时,我收到此错误:

The following classes could not be instantiated:
- com.google.android.gms.ads.AdView (Open Class, Show Exception)
 Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE

我正在使用AndroidStudio,我根据sdk - / extras / google-play-services / samples / ads上的示例做了一切。

有人可以帮我吗?

的xml:

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

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

android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myfirstapp.MainActivity$PlaceholderFragment"
android:background="@drawable/fartpic"
>
<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="@string/ad_unit_id"/>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="@string/copy_right" />

清单:

<?xml version="1.0" encoding="utf-8"?>

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </activity>

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

的build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'android'
android {
    compileSdkVersion 19
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.pachu.fartsounds"
        minSdkVersion 9
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
    compile 'com.google.android.gms:play-services:5.0.89'
}

1 个答案:

答案 0 :(得分:3)

尝试在Intellij Community Edition IDE中预览自定义视图AdView时遇到了同样的问题。似乎有一个错误或者至少是预览 - 不友好的&#34;代码在 导致此问题的AdView类的最新版本,因此我降低了依赖性 在我的gradle文件(基于项目的Intellij)中使用版本4.x,如下所示:

compile 'com.google.android.gms:play-services:5.+'

compile 'com.google.android.gms:play-services:4.+'

具体来说,我将gradle文件的依赖项部分更改为:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:4.+'
}

我不认为这是正确的解决方案(如果尚未提交错误报告,我将尝试提交错误报告),因为Google现在需要使用&# 34;最新&#34;截至2014年8月31日的Google Play服务SDK。