android 间隙广告返回空值

q0qdq0h2  于 2022-11-20  发布在  Android
关注(0)|答案(1)|浏览(157)

我编写了一个名为“Trumple Run”的安卓应用程序。就像名字说的那样,这是一个跳跃和运行游戏,我想尽快发布它。在此之前,我想实现AdMob的间隙广告。我遵循了广告实现的官方指南,但当我试图显示添加时,应用程序总是崩溃。以下是我的MainActivity(信息:我删除了与问题无关的代码;方法addzeigen()是从另一个类调用的)

public class Main_activity extends Activity {

private InterstitialAd mInterstitial;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //Fullscreen
    DisplayMetrics metrics = getResources().getDisplayMetrics(); //Display größe herausfinden
    V.Bildbreite = metrics.widthPixels;
    V.Bildhöhe = metrics.heightPixels;

    addladen();

    V.v = new View(this);

    setContentView(V.v);

}


public void addladen(){
    mInterstitial = new InterstitialAd(this);
    mInterstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    AdRequest request = new AdRequest.Builder().build();
    mInterstitial.loadAd(request);
}

public void addzeigen(){
    if(mInterstitial.isLoaded()){
        mInterstitial.show();
        addladen();
    }

}

我查看了StackTrace以查找问题的原因,这两行指示填隙尚未示例化(返回空值)。

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.android.gms.ads.InterstitialAd.isLoaded()' on a null object reference

因此,我在addladen()方法中添加了一个空值检查器来防止崩溃。

public void addzeigen(){
    if(mInterstitial != null && mInterstitial.isLoaded()){
        mInterstitial.show();
        addladen();
    }

}

但是为什么我的Interstitial对象是空的?我在addladen()方法中没有以正确的方式示例化它吗?我已经在互联网上搜索了很长时间,但没有找到解决方案,并询问了AdMob支持,但他们说我应该在这里问,因为这是一个实现问题。我必须如何修改我的代码,以确保我的Interstitial不是空的?如果你能帮助我,我将非常高兴。

lb3vh1jj

lb3vh1jj1#

打开应用程序时创建应用程序,立即加载

public class MyApplication extends Application {
public InterstitialAd mInterstitialAd;
private AppOpenManager appOpenAdManager;
private Activity currentActivity;
private int check = 0;
public AdLoader adLoader;
@Override
public void onCreate() {
    super.onCreate();
    MobileAds.initialize(
            this,
            new OnInitializationCompleteListener() {
                @Override
                public void onInitializationComplete(
                        @NonNull InitializationStatus initializationStatus) {
                }
            });
    appOpenAdManager = new AppOpenManager();
    loadInterstitial(AD_UNIT_ID_INTERSTITIAL_ADS);
}

public void loadInterstitial(String id) {
    AdRequest adRequest = new AdRequest.Builder().build();
    InterstitialAd.load(
            this,
            id,
            adRequest,
            new InterstitialAdLoadCallback() {
                @Override
                public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                    mInterstitialAd = interstitialAd;
                    interstitialAd.setFullScreenContentCallback(
                            new FullScreenContentCallback() {
                                @Override
                                public void onAdDismissedFullScreenContent() {
                                    mInterstitialAd = null;
                                }

                                @Override
                                public void onAdFailedToShowFullScreenContent(AdError adError) {
                                    // Called when fullscreen content failed to show.
                                    // Make sure to set your reference to null so you don't
                                    // show it a second time.
                                    mInterstitialAd = null;
                                }

                                @Override
                                public void onAdShowedFullScreenContent() {
                                }
                            });
                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    mInterstitialAd = null;
                }
            });
}

}然后创建类检查加载和打开应用程序时发生的异常

public class LoadInterstitialAds {
private static LoadInterstitialAds instance;
private InterstitialAd mInterstitialAd;

public static LoadInterstitialAds getInstance() {
    if (instance == null) {
        return new LoadInterstitialAds();
    }
    return instance;
}

public void openAdsThenOpenActivity(Activity activity, InterstitialAdsListener listener) {
    Application application = activity.getApplication();
    if (application instanceof MyApplication) {
        LoadInterstitialAds.this.mInterstitialAd = ((MyApplication) application).mInterstitialAd;
        if (LoadInterstitialAds.this.mInterstitialAd != null) {
            LoadInterstitialAds.this.mInterstitialAd.show(activity);
            LoadInterstitialAds.this.mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {

                @Override
                public void onAdDismissedFullScreenContent() {
                    listener.onStartActivity();
                    ((MyApplication) application).loadInterstitial(AD_UNIT_ID_INTERSTITIAL_ADS);
                }

                @Override
                public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
                    ((MyApplication) application).loadInterstitial(AD_UNIT_ID_INTERSTITIAL_ADS);
                    listener.onStartActivity();
                }

            });
        } else {
            ((MyApplication) application).loadInterstitial(AD_UNIT_ID_INTERSTITIAL_ADS);
            listener.onStartActivity();
        }
    }
}

}创建接口侦听器以方便调用

public interface InterstitialAdsListener {
void onStartActivity();

},最后,在您想要显示的任何位置运行此代码

LoadInterstitialAds loadInterstitialAds = LoadInterstitialAds.getInstance();
        loadInterstitialAds.openAdsThenOpenActivity(getActivity(), ()->{
           // your process
        });

相关问题