adMob横幅广告已加载但未显示

时间:2019-11-27 23:36:37

标签: java android firebase admob banner

我试图在我的带有Firebase的应用程序中显示测试横幅广告,但它们不会显示在屏幕上。 到达AdListener在onAdLoaded()上运行的地步,但广告本身从未被显示出来。这里有很多类似的问题,但是没有一种解决方案对我有用。

AdView XML:

    <com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="320dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id" />

Java活动:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MobileAds.initialize(this, "ca-app-pub-27141906xxxxxxxx~xxxxxxxxx060");

    setContentView(R.layout.activity_main);
    mAdView = findViewById(R.id.adView);
    AdRequest adRequest = new 
    AdRequest.Builder().addTestDevice("9771367910301885D43xxxxxxxxxxxxxxxx").build();


    mAdView.loadAd(adRequest);
    mAdView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            // Code to be executed when an ad finishes loading.
           Log.e("umbchat","AD IS LOADED!");
            super.onAdLoaded();

            if (mAdView.getVisibility() == View.GONE) {
                mAdView.setVisibility(View.VISIBLE);
            }
        }

app build.gradle:

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'

//GOOGLE ADS CODE!!!

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.navigation:navigation-fragment:2.0.0'
implementation 'androidx.navigation:navigation-ui:2.0.0'

implementation 'com.google.firebase:firebase-ads:17.0.0'

android清单:

    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-2714190698359503~4608788060" />

    <activity
        android:name=".ui.login.login_activity"
        android:label="@string/title_activity_login_activity" />
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.google.android.gms.ads.AdActivity"

    android:configChanges="keyboard|keyboardHidden|orientation|
                       screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent"/>
</application>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

我尝试过更改AdView,更改xmlns,初始化MobileAd,添加测试设备以及其他一些我现在不记得的事情。

1 个答案:

答案 0 :(得分:0)

您应该遵循本教程Mobile Ads SDK (Android)