在 android 工作室中膨胀 class com.google.android.gms.ads.AdView 时出错
Error inflating class com.google.android.gms.ads.AdView in android studio
我正在尝试向片段添加 admod。在 build.gradle 中添加依赖项后,我仍然无法膨胀 admod class 并在我的 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
build.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.Change它是最新的。
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"/>
我正在尝试向片段添加 admod。在 build.gradle 中添加依赖项后,我仍然无法膨胀 admod class 并在我的 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
build.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.Change它是最新的。
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"/>