java—在android studio中运行admob广告时遇到的问题

1u4esq0p  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(219)

当我试着运行android工作室时 Adutilities.java 下面显示的文件显示错误 MobileAds.initialize(context, context.getResources().getString(R.string.app_ad_id)); 还有下面的部分

mInterstitialAd = new InterstitialAd(activity);
mInterstitialAd.setAdUnitId(activity.getResources().getString(R.string.interstitial_ad_unit_id));
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);

显示运行admob中间层ALADS时出错。此外,在全屏广告加载显示错误

if (mInterstitialAd.isLoaded()) {
    mInterstitialAd.show();
    mClickCount = 0;
    return true;
}

我请求帮助解决此错误。adutilities.java的详细代码如下所示

package com.uses.down.utility;

import android.app.Activity;
import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;

import com.civilengg.studynotes.R;
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.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.interstitial.InterstitialAd;

import com.google.android.gms.ads.MobileAds;

import com.civilengg.studynotes.data.constant.AppConstant;

public class AdsUtilities {

    private static AdsUtilities mAdsUtilities;

    private InterstitialAd mInterstitialAd;

    private boolean mDisableBannerAd = false, mDisableInterstitialAd = false;

    private static int mClickCount = 0;

    private AdsUtilities(Context context ) {
        MobileAds.initialize(context, context.getResources().getString(R.string.app_ad_id));

    }

    public static AdsUtilities getInstance(Context context) {
        if (mAdsUtilities == null) {
            mAdsUtilities = new AdsUtilities(context);
        }
        return mAdsUtilities;
    }

    public void showBannerAd(final AdView mAdView) {
        if (mDisableBannerAd) {
            mAdView.setVisibility(View.GONE);
        } else {
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);

            mAdView.setAdListener(new AdListener() {
                @Override
                public void onAdLoaded() {
                    super.onAdLoaded();
                    mAdView.setVisibility(View.VISIBLE);
                }

                @Override
                public void onAdFailedToLoad(int errorCode) {
                    super.onAdFailedToLoad(errorCode);
                    mAdView.setVisibility(View.GONE);
                }
            });
        }
    }

    public void loadFullScreenAd(Activity activity) {
        if (!mDisableInterstitialAd) {
            mClickCount++;
            if (mClickCount >= AppConstant.CLICK_COUNT) {
                mInterstitialAd = new InterstitialAd(activity);
                mInterstitialAd.setAdUnitId(activity.getResources().getString(R.string.interstitial_ad_unit_id));
                AdRequest adRequest = new AdRequest.Builder().build();
                mInterstitialAd.loadAd(adRequest);

            }
        }
    }

    public boolean showFullScreenAd() {
        if (!mDisableInterstitialAd) {
            if (mInterstitialAd != null) {

                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                    mClickCount = 0;
                    return true;
                }
            }
        }
        return false;
    }

    public void disableBannerAd() {
        this.mDisableBannerAd = true;
    }

    public void disableInterstitialAd() {
        this.mDisableInterstitialAd = true;
    }    
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题