我正在创建一个应用,它带有广告横幅。 使用ad mob,我创建了广告ID,并将其插入到我的应用中。
MainActivity.java
package com.example.adtest;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity{
private AdView testAdView;
private AdView mAdView1;
private AdView mAdView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
testAdView = findViewById(R.id.testAdView);
AdRequest adRequest = new AdRequest.Builder().build();
testAdView.loadAd(adRequest);
testAdView.setAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) {
Log.d(TAG, "Error code for test Ad View: " + errorCode);
}
});
mAdView1 = findViewById(R.id.adView1);
AdRequest adRequest1 = new AdRequest.Builder().build();
mAdView1.loadAd(adRequest1);
mAdView1.setAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) {
Log.d(TAG, "Error code for Ad View 1: " + errorCode);
}
});
mAdView2 = findViewById(R.id.adView2);
AdRequest adRequest2 = new AdRequest.Builder().build();
mAdView2.loadAd(adRequest2);
mAdView2.setAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) {
Log.d(TAG, "Error code for Ad View 2: " + errorCode);
}
});
}
}
activity_main.xml
<androidx.drawerlayout.widget.DrawerLayout 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"
tools:context=".MainActivity"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
android:background="@drawable/bacround_gradient">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/testAdView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="<ad unit here>">
</com.google.android.gms.ads.AdView>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="<ad unit here>">
</com.google.android.gms.ads.AdView>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/navigation_view"
app:headerLayout="@layout/header"
android:layout_gravity="start"
app:menu="@menu/navigation_menu"/>
</androidx.drawerlayout.widget.DrawerLayout>
我添加了一个测试广告和两个横幅广告,但仅显示了测试广告,下面是其他两个横幅广告的日志
2020-08-10 22:40:15.561 14414-14414/com.example.adtest D/MainActivity: Error code for Ad View 2: 3
2020-08-10 22:40:15.568 14414-14414/com.example.adtest D/MainActivity: Error code for Ad View 1: 3
我的admob帐户一切正常 我尚未将我的应用发布到Play商店。谁能告诉我哪里出问题了?