问题描述
我在我的应用中实现了Google admob,并尝试对其进行测试。 这些是我遵循的步骤:https://firebase.google.com/docs/admob/android/quick-start
这是我的清单:
<application
.
.
.>
.
.
<Meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-**********~**********"/>
<application/>
这是我添加到gradle中的依赖项:
implementation 'com.google.android.gms:play-services-ads:19.5.0'
MobileAds.initialize(this);
这是我的带有admob视图的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:background="@color/grey_f2"
tools:ignore="UnusedAttribute">
<androidx.core.widget.nestedScrollView
android:id="@+id/workout_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
>
<LinearLayout
android:id="@+id/ll_profile_stats"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:id="@+id/stats_sharable_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:background="@color/white"
android:orientation="vertical"
android:paddingBottom="15dp"
android:paddingTop="25dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="13dp"
android:layout_marginRight="13dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/img_profile_stats"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerInParent="true"
android:layout_marginBottom="8dp"
android:src="@drawable/placeholder_profile"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:transitionName="profile" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/img_profile_stats_dup"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerInParent="true"
android:layout_marginBottom="8dp"
android:src="@drawable/placeholder_profile"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:transitionName="profile" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="96dp"
android:layout_height="24dp"
android:layout_alignParentBottom="true"
android:src="@drawable/max_streak_bg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<TextView
android:id="@+id/tv_level_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:fontFamily="@font/lato_bold"
android:letterSpacing="0.01"
android:textColor="@color/white"
android:textSize="11sp"
app:layout_constraintBottom_toBottomOf="@+id/imageView2"
app:layout_constraintEnd_toEndOf="@+id/imageView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView2"
app:layout_constraintVertical_bias="0.3"
tools:text="LEVEL 7 " />
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:orientation="vertical">
<com.sharesmile.share.views.LBKTextView
android:id="@+id/tv_profile_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:textColor="@color/black_a3"
android:textSize="20sp"
tools:text="Ankit Maheshwari"
android:letterSpacing="0.01" />
<com.sharesmile.share.views.LBTextView
android:id="@+id/tv_profile_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:textColor="@color/black_a5"
android:textSize="12sp"
tools:text="EDUCATION MASTER"
android:letterSpacing="0.01"
/>
<TextView
android:id="@+id/user_bio_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLength="140"
android:ellipsize="end"
android:gravity="center_vertical"
android:text="@string/def_about_me"
android:textColor="@color/black_64"
android:textSize="11sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:layout_marginRight="22dp"
android:layout_marginTop="15dp">
<com.sharesmile.share.views.LBTextView
android:id="@+id/tv_impact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="1"
android:text="\u20B9 657"
android:textColor="@color/clr_6e"
android:textSize="30sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/total_workouts_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lato_bold"
android:gravity="bottom"
android:textColor="@color/black_57"
android:textSize="16sp"
tools:text="1563" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lato_regular"
android:gravity="top"
android:text="@string/profile_workouts"
android:textColor="@color/black_5e"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/non_impact_distance_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="25dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/total_distance_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lato_bold"
android:gravity="bottom"
android:textColor="@color/black_57"
android:textSize="16sp"
tools:text="1563" />
<TextView
android:id="@+id/total_distance_unit_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:fontFamily="@font/lato_bold"
android:gravity="bottom"
android:text="km"
android:textColor="@color/black_37"
android:textSize="11.4sp" />
</LinearLayout>
<TextView
android:id="@+id/distance_label_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lato_regular"
android:gravity="top"
android:text="@string/profile_distance"
android:textColor="@color/black_5e"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="13dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="13dp"
android:background="@color/grey_f2" />
<RelativeLayout
android:id="@+id/streak_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/shoe_streak_icon" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/tv_streak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lato_bold"
android:gravity="top"
android:textColor="@color/black_57"
android:textSize="16sp"
tools:text="63" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-2dp"
android:fontFamily="@font/lato_regular"
android:gravity="top"
android:text="@string/profile_streak"
android:textColor="@color/black_5e"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/shoe_max_streak_icon" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/tv_max_streak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lato_bold"
android:gravity="top"
android:textColor="@color/black_57"
android:textSize="16sp"
tools:text="63" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-2dp"
android:fontFamily="@font/lato_regular"
android:gravity="top"
android:text="@string/profile_max_streak"
android:textColor="@color/black_5e"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/meals_shared_linear_layout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/clr_328f6c"
android:paddingLeft="27dp"
android:paddingRight="29dp"
android:layout_marginBottom="6dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<com.sharesmile.share.views.LBTextView
android:id="@+id/tv_meal_count"
android:text="0 "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/white"
/>
<com.sharesmile.share.views.LBTextView
android:id="@+id/right_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:gravity="center"
android:text=">"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<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_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-8172285899785286/**********">
</com.google.android.gms.ads.AdView>
</LinearLayout>
<LinearLayout
android:id="@+id/achievements_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="23dp"
android:layout_marginTop="45dp">
<com.sharesmile.share.views.LBTextView
style="@style/profile_stats_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/profile_stats_achievements" />
</RelativeLayout>
</androidx.core.widget.nestedScrollView>
</FrameLayout>
这是我使用admob的片段代码:
AdRequest adRequest = new AdRequest.Builder()
.build();
Timber.d("TEST123 : "+adRequest.isTestDevice(getContext()));
adView.loadAd(adRequest);
adView.setAdListener(new AdListener(){
@Override
public void onAdClosed() {
super.onAdClosed();
Timber.d("TEST123 : AdClosed");
}
@Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
Timber.d("TEST123 : onAdFailedToLoad");
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
Timber.d("TEST123 : onAdFailedToLoad : "+loadAdError.toString());
}
@Override
public void onAdLeftApplication() {
super.onAdLeftApplication();
Timber.d("TEST123 : onAdLeftApplication");
}
@Override
public void onAdOpened() {
super.onAdOpened();
Timber.d("TEST123 : onAdOpened");
}
@Override
public void onAdLoaded() {
super.onAdLoaded();
Timber.d("TEST123 : onAdLoaded");
}
@Override
public void onAdClicked() {
super.onAdClicked();
Timber.d("TEST123 : onAdClicked");
}
@Override
public void onAdImpression() {
super.onAdImpression();
Timber.d("TEST123 : onAdImpression");
}
});
onAdFailedToLoad : {
"Code": 3,"Message": "No ad config.","Domain": "com.google.android.gms.ads","Cause": "null","Response Info": {
"Response ID": "null","Mediation Adapter Class Name": "","Adapter Responses": []
}
}
解决方法
1-确保清单正确。 元数据标记应位于应用标记(如活动)之内。
2-这对插页式广告很有用:
MobileAds.initialize(this,new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-********************");
AdRequest ad = new AdRequest.Builder().build();
mInterstitialAd.loadAd(ad);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
mInterstitialAd.show();
}
@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}
@Override
public void onAdOpened() {
// Code to be executed when the ad is displayed.
}
@Override
public void onAdClicked() {
// Code to be executed when the user clicks on an ad.
}
@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
@Override
public void onAdClosed() {
// Code to be executed when the interstitial ad is closed.
}
});
活动/片段上的mInterstitialAd声明为:
private InterstitialAd mInterstitialAd;