【问题标题】:GDPR Consent form crashing: WindowManager BadTokenExceptionGDPR 同意书崩溃:WindowManager BadTokenException
【发布时间】:2018-12-03 19:29:20
【问题描述】:

我正在显示Google's GDPR consent form,我注意到很多这样的报告:

Fatal Exception: android.view.WindowManager$BadTokenException
Unable to add window -- token android.os.BinderProxy@38734f2 is not valid; is your activity running?
com.my.project.MainActivity$4.onConsentFormLoaded

作为上下文我使用MainActivity.this:

private void displayConsentForm() {
    consentForm = new ConsentForm.Builder(MainActivity.this, GeneralUtils.getAppsPrivacyPolicy())
            .withListener(new ConsentFormListener() {
                @Override
                public void onConsentFormLoaded() {
                    consentForm.show(); // crashing here for some users
                }

                @Override
                public void onConsentFormOpened() { }

                @Override
                public void onConsentFormClosed(
                    ConsentStatus consentStatus, Boolean userPrefersAdFree) {

                    if(userPrefersAdFree) {
                            ConsentInformation.getInstance(MainActivity.this)
                                    .setConsentStatus(NON_PERSONALIZED);
                    } else {
                        ConsentInformation.getInstance(MainActivity.this)
                                .setConsentStatus(consentStatus);
                    }

                    initAds();
                }

                @Override
                public void onConsentFormError(String errorDescription) {
                    Log.e("Error",errorDescription);
                }
            })
            .withPersonalizedAdsOption()
            .withNonPersonalizedAdsOption()
            .withAdFreeOption()
            .build();

    consentForm.load();
}

这里是额外的 Firebase 崩溃报告:

为什么会发生这种情况以及如何预防?我不确定在consentForm.show() 之前要进行哪些额外检查,并且我无法重现该问题。如果我在显示表格之前打勾就足够了:

if(!MainActivity.this.isFinishing() && !MainActivity.this.isDestroyed())

?

【问题讨论】:

  • 发生这种情况的常见情况是尝试在活动开始完成后显示一个对话框。通常我只是捕获并忽略异常。

标签: android android-activity android-context android-windowmanager


【解决方案1】:

解决此问题的最简单方法是在 consentForm.show() 周围放置一个 try-catch 块并捕获 BadTokenException。

它不是很干净,但很可能是在 Activity 完成时发生的(也许用户在加载对话框时从最近关闭了应用程序)。

如果这是我的项目,我会首先尝试添加您拥有的 if 语句(尽管您不需要 MainActivity.this. 部分;您可以直接调用 isFinishing()isDestroyed())。由于您引用的是 Activity 上下文,因此应该处理它。

但是,如果它仍然崩溃,您应该首先考虑重现它。尝试在调用 displayConsentForm() 之前到达,然后从“最近”关闭应用程序。玩弄时间,你可能会重现崩溃。如果没有,那么只需添加 try-catch。 Activity 没有显示,因为它抛出了那个错误,所以用户实际上并不在应用程序中。

【讨论】:

  • 谢谢。这意味着坠机并不严重,这让我的心情更加平静。我将添加所讨论的其他措施。
猜你喜欢
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-06
相关资源
最近更新 更多