在Android 7

时间:2017-09-04 05:22:36

标签: java android

Google Play会显示由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();

造成这次事故的原因是什么?如果我有办法重现崩溃,它也会有所帮助。该应用的完整来源是available on Github

2 个答案:

答案 0 :(得分:1)

如果应用程序正在关闭,或者某种程度上是在后台,可能会出现这样的问题。 您应该检查您的活动是否未被关闭(例如,调用了onDestroy),以及它是否在前台(并且可见)处于活动状态。

有几种方法可以检查它是否在后台,这是一个应该有效的方法:

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

答案 1 :(得分:0)

好的,我发现了问题。在我的代码中,我正在做这样的事情(以更复杂的方式):

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

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