android:admob InterstitialAd在loadAd

时间:2017-04-05 16:11:25

标签: android crash admob interstitial

我正在使用Admob的InterstitialAd。我的应用程序仅在第一次调用时在“loadAd”崩溃,并且它不可重现(它在100-200次运行中发生一次或多次)。广告单元ID肯定是正确的。因此,如果loadAd在第一次调用时没有失败,那么在此次运行中它根本不会失败。

班级成员:

InterstitialAd mInterstitialAd;

的onCreate:

mInterstitialAd = new InterstitialAd(this);

mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_ad_unit_id));

mInterstitialAd.setAdListener(new AdListener() {
    @Override
    public void onAdClosed() {
        requestNewInterstitial();
        finish();
    }
});

requestNewInterstitial();

requestNewInterstitial方法:

private void requestNewInterstitial() {
    if (mInterstitialAd != null) {
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(getResources().getString(R.string.test_device))
                .build();

        if (adRequest == null) {
            return;
        }

        // HERE'S THE CRASH
        try {
            mInterstitialAd.loadAd(adRequest);
        }catch(Exception e) {
            return;
        }
    }
}

catch 无法捕获“loadAd”的崩溃。

我怎样才能抓住它或至少导致它不会让我的应用程序崩溃?我希望当loadAd失败时,不会显示任何广告。

logcat的:

A/libc: Fatal signal 5 (SIGTRAP), code 1 in tid 27794 (AdWorker(Defaul)
W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es

我不知道最后两行是否与此次崩溃相关,但第一行肯定是这样。

2 个答案:

答案 0 :(得分:0)

有时会发生什么事情,Ad会加载到缓冲区中,因此当您尝试调用loadAd时,它会崩溃 的onCreate:

    mInterstitialAd = new InterstitialAd(this);
    // set the ad unit ID
    mInterstitialAd.setAdUnitId(getString(R.string.ad_id));

    AdRequest adRequest2 = new AdRequest.Builder()
            .build();
    // Load ads into Interstitial Ads

    mInterstitialAd.loadAd(adRequest2);

    mInterstitialAd.setAdListener(new AdListener() {
        public void onAdLoaded() {
            showInterstitial();
        }
    }); 

在同一个班级:

private void showInterstitial() {
    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
    }
}

答案 1 :(得分:0)

<强> Activtiy_m1.java

ng-table-dynamic

<强> Advertise.java

public class Activity_m1 extends AppCompatActivity {

    InterstitialAd interstitial;
    Activity mactivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_m1);
        setTitle("ABCD");
        Advertise.displaym1(getApplicationContext());
    }

    @Override
    public void onBackPressed() {
            Advertise adv = new Advertise();
            adv.disp_adm1();
        finish();
    }
}