首页 > 解决方案 > Android:使用 admob 时,谷歌播放控制台崩溃显示非法状态异常

问题描述

在 google play 控制台中发生了很多这种崩溃,我不知道它来自哪里。我也实施了firebase crashlaytics,但也找不到足够的东西。但在谷歌游戏控制台生命体征中,这种情况发生了很多:

java.lang.IllegalStateException:    at androidx.fragment.app.Fragment.requireActivity(Fragment.java:17)
    at com.google.android.gms.ads.admanager.AdManagerAdRequest.zza(AdManagerAdRequest.java:0)
    <OR> at com.google.android.gms.ads.admanager.AdManagerInterstitialAd.load(AdManagerInterstitialAd.java:0)
    <OR> at com.navlab.recical.ui.home.HomeFragment.createInterstitialAd(HomeFragment.java:0)
    <OR> at com.navlab.recical.ui.home.HomeFragment.createInterstitialAd(HomeFragment.java:0)
    at com.navlab.recical.ui.home.HomeFragment.createNonPersonalisedAd(HomeFragment.java:0)
    at com.navlab.recical.ui.home.HomeFragment.access$400(HomeFragment.java:0)
    at com.navlab.recical.ui.home.HomeFragment$12.onInitializationComplete(HomeFragment.java:0)
    at com.google.android.gms.internal.ads.zzbhi.zzb(zzbhi.java:0)
    at com.google.android.gms.internal.ads.zzbrg.zzbT(zzbrg.java:9)
    at com.google.android.gms.internal.ads.zzow.onTransact(zzow.java:20)
    at android.os.Binder.transact(Binder.java:627)
    at zm.bb(:com.google.android.gms.policy_ads_fdr_dynamite@213806100@213806100000.397311113.397311113:2)
    at com.google.android.gms.ads.internal.initialization.f.e(:com.google.android.gms.policy_ads_fdr_dynamite@213806100@213806100000.397311113.397311113:0)
    at com.google.android.gms.ads.nonagon.initialization.c.run(:com.google.android.gms.policy_ads_fdr_dynamite@213806100@213806100000.397311113.397311113:2)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at atv.a(:com.google.android.gms.policy_ads_fdr_dynamite@213806100@213806100000.397311113.397311113:0)
    at com.google.android.gms.ads.internal.util.f.a(:com.google.android.gms.policy_ads_fdr_dynamite@213806100@213806100000.397311113.397311113:1)
    at atv.dispatchMessage(:com.google.android.gms.policy_ads_fdr_dynamite@213806100@213806100000.397311113.397311113:0)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6543)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:440)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:810)

代码:

  private void initAds(){
    if(!getPurchaseValueFromPref()){
    MobileAds.initialize(requireActivity(), new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(@NonNull InitializationStatus initializationStatus) {

            if(euConscent){ createPersonalisedAd();}
            else{ createNonPersonalisedAd();}
        }
    });
}}
private void createPersonalisedAd() {
    AdManagerAdRequest adRequest = new AdManagerAdRequest.Builder().build();
    createInterstitialAd(adRequest);
}
private void createNonPersonalisedAd() {

    Bundle networkExtrasBundle = new Bundle();
    networkExtrasBundle.putInt("rdp", 1);
    AdManagerAdRequest adRequest = (AdManagerAdRequest) new AdManagerAdRequest.Builder()
            .addNetworkExtrasBundle(AdMobAdapter.class, networkExtrasBundle)
            .build();
    createInterstitialAd(adRequest);
}
private  void createInterstitialAd(AdRequest adRequest){


    AdManagerInterstitialAd.load(requireActivity(),getResources().getString(R.string.str_iterstitial), (AdManagerAdRequest) adRequest,
            new AdManagerInterstitialAdLoadCallback() {
                @Override
                public void onAdLoaded(@NonNull AdManagerInterstitialAd interstitialAd) {
                    // The mAdManagerInterstitialAd reference will be null until
                    // an ad is loaded.
                    mAdManagerInterstitialAd = interstitialAd;
                    Log.i(TAG, "onAdLoaded");

                    ///// best place to callback is here coz its successfully loaded here
                    mAdManagerInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
                        @Override
                        public void onAdDismissedFullScreenContent() {
                            // Called when fullscreen content is dismissed.
                            Log.d("TAG", "The ad was dismissed.");
                            createInterstitialAd(adRequest);
                        }

                        @Override
                        public void onAdFailedToShowFullScreenContent(AdError adError) {
                            // Called when fullscreen content failed to show.
                            Log.d("TAG", "The ad failed to show.");
                        }

                        @Override
                        public void onAdShowedFullScreenContent() {
                            // Called when fullscreen content is shown.
                            // Make sure to set your reference to null so you don't
                            // show it a second time.
                            mAdManagerInterstitialAd = null;
                            Log.d("TAG", "The ad was shown.");
                        }
                    });
                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    // Handle the error
                    Log.i(TAG, loadAdError.getMessage());
                    mAdManagerInterstitialAd = null;
                }
            });


}

private void initAdsCallBack(){


    try {
        int adFrequency = getResources().getInteger(R.integer.addFrequency);
        click_counter++;
        // for showing ads once per session
        if (adFrequency == ONCE_PER_SESSION) {

            if (mAdManagerInterstitialAd != null && !getPurchaseValueFromPref() && click_counter == 1) {

                mAdManagerInterstitialAd.show(requireActivity());
                debugToast("ad shown");
            } else {
                     Log.e("TAG","Add didn't show for following reasons : ");
                     if(click_counter!=1){
                         showLog("Ad didn't show coz counter is not 1 ,");
                        
                     }
                     if(mAdManagerInterstitialAd==null){
                         showLog("mAdManager is NULL \n");
                        
                         debugToast("mAdManager is NULL");
                     }
            }
        }
        // for showing ads after certain numbers of clicks
        else {
            if (click_counter == adFrequency) {
                click_counter = 0;
            }

            if (mAdManagerInterstitialAd != null && !getPurchaseValueFromPref() && click_counter == 0) {
                mAdManagerInterstitialAd.show(requireActivity());
                debugToast("ad shown");
            } else {
                Log.d("TAG", "The interstitial ad wasn't ready yet.");
            }

        }
    }catch (Exception e ){Log.e("TAG","Exception : "+e);}
}

//implementation


 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     if(isAdded){initAds();}
     btn.setOnclickListener(this);
}

public void onClick(View view){
     initAdsCallBack();
}

这是从哪里来的?我究竟做错了什么 ?对不起大代码,但我无法弄清楚发生了什么。它在上周发生了 90 次。

标签: androidadmobcrash-reports

解决方案


推荐阅读