“android.view.WindowManager$BadTokenException: Unable to add window” on buider.show()

前端 未结 10 930
忘掉有多难
忘掉有多难 2020-11-22 10:43

From my main activity, I need to call an inner class and in a method within the class, I need to show AlertDialog. After dismissing it, when the OK

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 11:13

    I am creating Dialog in onCreate and using it with show and hide. For me the root cause was not dismissing onBackPressed, which was finishing the Home activity.

    @Override
    public void onBackPressed() {
    new AlertDialog.Builder(this)
                    .setTitle("Really Exit?")
                    .setMessage("Are you sure you want to exit?")
                    .setNegativeButton(android.R.string.no, null)
                    .setPositiveButton(android.R.string.yes,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    Home.this.finish();
                                    return;
                                }
                            }).create().show();
    

    I was finishing the Home Activity onBackPressed without closing / dismissing my dialogs.

    When I dismissed my dialogs the crash disappeared.

    new AlertDialog.Builder(this)
                    .setTitle("Really Exit?")
                    .setMessage("Are you sure you want to exit?")
                    .setNegativeButton(android.R.string.no, null)
                    .setPositiveButton(android.R.string.yes,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    networkErrorDialog.dismiss() ;
                                    homeLocationErrorDialog.dismiss() ;
                                    currentLocationErrorDialog.dismiss() ;
                                    Home.this.finish();
                                    return;
                                }
                            }).create().show();
    

提交回复
热议问题