gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:30:04 27 4
gpt4 key购买 nike

我正在尝试将 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com