在android 7上显示alertdialog时windowmanager$badtokenexception

4si2a6ki  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(310)

googleplay显示了许多由windowmanager$badtokenexception引起的崩溃报告。
完整日志:

java.lang.RuntimeException: 
    at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3344)
    at android.app.ActivityThread.-wrap21 (ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1583)
    at android.os.Handler.dispatchMessage (Handler.java:102)
    at android.os.Looper.loop (Looper.java:154)
    at android.app.ActivityThread.main (ActivityThread.java:6121)
    at java.lang.reflect.Method.invoke (Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:889)
    at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:779)
Caused by: android.view.WindowManager$BadTokenException: 
    at android.view.ViewRootImpl.setView (ViewRootImpl.java:679)
    at android.view.WindowManagerGlobal.addView (WindowManagerGlobal.java:342)
    at android.view.WindowManagerImpl.addView (WindowManagerImpl.java:93)
    at android.app.Dialog.show (Dialog.java:329)
    at android.app.AlertDialog$Builder.show (AlertDialog.java:1112)
    at com.nutomic.syncthingandroid.activities.SyncthingActivity.showLoadingDialog (SyncthingActivity.java:142)
    at com.nutomic.syncthingandroid.activities.SyncthingActivity.onApiChange (SyncthingActivity.java:88)
    at com.nutomic.syncthingandroid.activities.SyncthingActivity.access$lambda$1 (SyncthingActivity.java)
    at com.nutomic.syncthingandroid.activities.SyncthingActivity$$Lambda$2.onApiChange (Unknown Source)
    at com.nutomic.syncthingandroid.service.SyncthingService.onApiChange (SyncthingService.java:530)
    at com.nutomic.syncthingandroid.service.SyncthingService.updateState (SyncthingService.java:246)
    at com.nutomic.syncthingandroid.service.SyncthingService.onStartCommand (SyncthingService.java:181)
    at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3327)

相关代码:

if (isFinishing() || mLoadingDialog != null)
    return;

LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_loading, null);
TextView loadingText = (TextView) dialogLayout.findViewById(R.id.loading_text);
loadingText.setText((getIntent().getBooleanExtra(EXTRA_FIRST_START, false))
        ? R.string.web_gui_creating_key
        : R.string.api_loading);

mLoadingDialog = new AlertDialog.Builder(this)
        .setCancelable(false)
        .setView(dialogLayout)
        .show();

这次坠机的原因可能是什么?如果我有办法重现这次坠机也会有所帮助。github上提供了该应用程序的完整源代码。

um6iljoc

um6iljoc1#

好吧,我找到问题了。在我的代码中,我是这样做的(以一种更复杂的方式):

if (isFinishing())
    return;
new AlertDialog.Builder(this).show();
Thread.sleep(1000);

show()方法调用正常完成,但对话框仅在睡眠完成后附加到活动。因此,如果您有这个问题,请确保您没有在主线程上执行任何长时间运行的操作。

xv8emn3q

xv8emn3q2#

如果应用程序正在关闭或处于后台,则可能会出现这样的问题。您应该检查您的活动是否未关闭(例如,调用了ondestroy),以及它是否在前台处于活动状态(并且可见)。
有几个选项可以用来检查它是否在后台,下面是一个应该可以工作的选项:

RunningAppProcessInfo myProcess = new RunningAppProcessInfo();
ActivityManager.getMyMemoryState(myProcess);
isInBackground = myProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND;

相关问题