alertDialogBu​​ilder 仅在重新启动 APP 时崩溃

alertDialogBuilder crashes only when relaunching the APP

我有一个 "alertDialogBuilder" 可以在按下按钮时重命名条目。当应用程序刚打开时,这工作正常。但是如果我按下后退按钮(意味着应用程序被最小化并且我回到 Android 主屏幕),当我重新启动应用程序并按下按钮时,这次应用程序崩溃了。每次都会发生这种情况,我不知道如何调试它。我检查了生命周期, "onPause" 和 "onStop" 在按下后退按钮时被调用。但我不明白为什么这应该是个问题。

有什么想法吗?

这是我在助手中启动提示对话的代码 class:

public void loadPromptInput(Context promptcontext, final OnOkGetText onOk, String InitialTxt) {

    //pathText.setText("Prompt input");
    LayoutInflater li = LayoutInflater.from(promptcontext);
    View promptsView = li.inflate(R.layout.prompts_dialog, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( promptcontext);

    // set prompts.xml to alertdialog builder
    alertDialogBuilder.setView(promptsView);

    final EditText userInput = (EditText) promptsView
            .findViewById(R.id.editTextDialogUserInput);
    userInput.setText("");
    userInput.append(InitialTxt);
      alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            onOk.hereIsYouText(userInput.getText().toString());
                        }
                    })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // show it
    alertDialog.show();

    // make the keyboard shown by default
    alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

E/AndroidRuntime: FATAL EXCEPTION: main Process: com...., PID: 31622 android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@423c9940 is not valid; is your activity running? at android.view.ViewRootImpl.setView(ViewRootImpl.java:532) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) at android.app.Dialog.show(Dialog.java:286) at com....myUtils.loadPromptInput(myUtils.java:71) at com....MainActivity.onReceive(MainActivity.java:557) at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:308) at android.support.v4.content.LocalBroadcastManager.access[=11=]0(LocalBroadcastManager.java:46) at android.support.v4.content.LocalBroadcastManager.handleMessage(LocalBroadcastManager.java:118) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5095) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) at com.zte.heartyservice.SCC.FrameworkBridge.main(FrameworkBridge.java:136) at dalvik.system.NativeStart.main(Native Meth

od)

查看您的代码以了解您调用的位置会很有帮助:

void loadPromptInput(Context promptcontext...

...很可能您正在作为参数传递一个不再有效的上下文实例。

无论如何在调用您的方法之前检查 activity 是否正在完成:

//in a fragment
if(getActivity() != null && !getActivity().isFinishing()) {
    loadPromptInput(getActivity()...
}

//in an activity
if(!isFinishing()) {
    loadPromptInput(this...
}