java - 在 android studio 中膨胀类 com.google.android.gms.ads.AdView 时出错

标签 java android inflate-exception adview

我正在尝试将 admod 添加到 fragment 。在 build.gradle 中添加依赖项后,我仍然无法膨胀 admod 类并在 XML 上显示 admod。知道我做错了什么吗?我是否缺少任何设置?

错误信息

android.view.InflateException: Binary XML file line #1095 in com.centslife.development:layout/mockup_spotreceiptdetail: Binary XML file line #1095 in com.centslife.development:layout/mockup_spotreceiptdetail: Error inflating class com.google.android.gms.ads.AdView
    Caused by: android.view.InflateException: Binary XML file line #1095 in com.centslife.development:layout/mockup_spotreceiptdetail: Error inflating class com.google.android.gms.ads.AdView

构建.gradle

implementation 'com.google.android.gms:play-services-ads:10.2.1'

XML

        <com.google.android.gms.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner_ad_unit_id"></com.google.android.gms.ads.AdView>

更新

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.centslife.development"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.Home.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

        }
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.vectordrawable:vectordrawable:1.0.1'
    implementation 'androidx.navigation:navigation-fragment:2.0.0'
    implementation 'androidx.navigation:navigation-ui:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.firebase:firebase-firestore:17.1.2'
    testImplementation 'junit:junit:4.12'
    implementation "androidx.cardview:cardview:1.0.0"
    def roomVersion = "2.2.5"
    def archLifecycleVersion = '2.2.0'
    def coreTestingVersion = '2.1.0'
    implementation "androidx.room:room-runtime:$roomVersion"
    annotationProcessor "androidx.room:room-compiler:$roomVersion"
    androidTestImplementation "androidx.room:room-testing:$roomVersion"
    implementation "androidx.lifecycle:lifecycle-extensions:$archLifecycleVersion"
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$archLifecycleVersion"
    androidTestImplementation "androidx.arch.core:core-testing:$coreTestingVersion"
    implementation 'com.facebook.shimmer:shimmer:0.5.0'
    implementation 'com.google.android.gms:play-services-ads:10.2.1'


}


Update2 完整 XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00FFFFFF"
                app:srcCompat="@drawable/sample"
                tools:visibility="visible" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.google.android.gms.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner_ad_unit_id"> 
            </com.google.android.gms.ads.AdView>

    </LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

最佳答案

您使用的广告 sdk 版本非常旧。请将其更改为最新版本。

实现“com.google.android.gms:play-services-ads:19.1.0” 此外,您还必须在 list 文件的应用程序标签中添加元数据

<meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>

你可以这样尝试

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

        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id"/> 

关于java - 在 android studio 中膨胀类 com.google.android.gms.ads.AdView 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62065114/

相关文章:

java - getView如何确定回收的convertView在List中的位置

android - 无法将 JSON 解析的数据加载到 ArrayList 中并显示在 ListView 上

Android Facebook 登录在注销后总是询问用户

android - 尝试在获取此信息(android.view.InflateException : Binary XML. 。)中为 ListView 的每一行膨胀不同的 View 。

Android:InflateException 问题(膨胀类 android.support.v7.widget.Toolbar 时出错)

java - 如何让图像跟随鼠标移动?

java - 下拉菜单未按预期工作

android - 如何向右或向左滑动以获取从 ListView Intent 开始的下一个 Activity 详细信息

android - 在谷歌地图 fragment 中膨胀 XML 时出错

java - 使用java从网页中提取数据