繁体   English   中英

Android无法实例化以下类:-com.google.android.gms.ads.AdView(打开类,显示异常)

[英]Android The following classes could not be instantiated: - com.google.android.gms.ads.AdView (Open Class, Show Exception)

我试图将广告放在我的应用程序上,但是当我将广告放在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,而Im根据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'
}

尝试在Intellij Community Edition IDE中预览自定义视图AdView时遇到相同的问题。 最新版本的AdView类中似乎存在错误或至少“预览不友好”代码导致了此问题,因此我将gradle文件(基于Intellij项目的)中的依赖项降级为使用版本4。 x像这样:

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

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

具体来说,我将gradle文件的dependencies部分更改为如下所示:

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

我认为这不是适当的解决方案(如果尚未提交错误报告,我将尝试提交错误报告),因为Google现在要求从8 /开始使用“最新的” Google Play服务SDK。 31/2014。

暂无
暂无

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

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