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

时间:2017-12-07 16:55:06

标签: android crash relaunch

我有一个“alertDialogBu​​ilder”来在按下按钮时重命名条目。当应用程序刚刚打开时,这可以正常工作。但是,如果我按下后退按钮(意味着应用程序已最小化,我回到Android主屏幕),当我重新启动应用程序并按下按钮时,这次APP崩溃了。每次都会发生这种情况,我不知道如何调试它。我已经检查了生命周期,按下后退按钮时会调用“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:致命异常:主要                                                                                    过程:com ....,PID:31622                                                                                    android.view.WindowManager $ BadTokenException:无法添加窗口 -   令牌android.os.BinderProxy@423c9940无效;是你的活动   运行?                                                                                        在android.view.ViewRootImpl.setView(ViewRootImpl.java:532)                                                                                        在   android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)                                                                                        在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)                                                                                        在android.app.Dialog.show(Dialog.java:286)                                                                                        在   COM .... myUtils.loadPromptInput(myUtils.java:71)                                                                                        在   COM .... MainActivity $ 6.onReceive(MainActivity.java:557)                                                                                        在   android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:308)                                                                                        在   android.support.v4.content.LocalBroadcastManager.access $ 000(LocalBroadcastManager.java:46)                                                                                        在   android.support.v4.content.LocalBroadcastManager $ 1.handleMessage(LocalBroadcastManager.java:118)                                                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                        在android.os.Looper.loop(Looper.java:136)                                                                                        在android.app.ActivityThread.main(ActivityThread.java:5095)                                                                                        at java.lang.reflect.Method.invokeNative(Native Method)                                                                                        在java.lang.reflect.Method.invoke(Method.java:515)                                                                                        在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)                                                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)                                                                                        在   com.zte.heartyservice.SCC.FrameworkBridge.main(FrameworkBridge.java:136)                                                                                        在dalvik.system.NativeStart.main(Native Meth

OD)

1 个答案:

答案 0 :(得分:0)

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

void loadPromptInput(Context promptcontext...

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

在调用方法之前的任何情况下,检查活动是否结束:

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

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