【发布时间】: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