-1

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

有任何想法吗?

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

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; 您的活动正在运行吗?在 android.view.ViewRootImpl.setView(ViewRootImpl.java:532) 在 android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259) 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:

od)

4

1 回答 1

0

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

void loadPromptInput(Context promptcontext...

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

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

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

//in an activity
if(!isFinishing()) {
    loadPromptInput(this...
}
于 2017-12-07T17:33:17.413 回答