java - 在 android studio 中膨胀类 com.google.android.gms.ads.AdView 时出错
问题描述
我正在尝试将 admod 添加到片段中。在 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>
解决方案
您正在使用非常旧版本的 ads sdk。将其更改为最新版本。
implementation 'com.google.android.gms:play-services-ads:19.1.0'
您还必须在清单文件的应用程序标签中添加元数据
<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"/>
推荐阅读
- python - Django Rest API - 无法发出 PUT 或 DELETE 请求
- html - 内部 css 在移动浏览器上的电子邮件模板中不起作用
- android - 如何创建一个自定义的 AlertDialog,它能够将输入的数据传回 Android 中的调用 Activity?
- playlist - youtube-dl:如何跳过播放列表中不可用片段的视频,但不中止播放列表下载?
- arrays - 在nodejs中转换json响应
- android - Firestore 安全规则:批处理时 PERMISSION DENIED
- android - 如何实现带有渐变背景和模糊阴影的圆形按钮?
- c# - 试图弄清楚 if 语句和 GetKeyDown 函数
- java - Spring `SavedRequest` 是否包含任何清理逻辑以避免重定向循环或类似情况?
- asdf-vm - 如何使用 asdf 作为 root 用户?