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

前端 未结 10 806
忘掉有多难
忘掉有多难 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:12

    I got this error, but mine was coming from the Toasts, not a Dialog.

    I have Activity and Fragments in my layout. Code for the Toast was in the Activity class. Fragments gets loaded before the Activity.

    I think the Toast code was hit before the Context/Activity finished initializing. I think it was the getApplicationContext() in the command Toast.makeText(getApplicationContext(), "onMenutItemActionCollapse called", Toast.LENGTH_SHORT).show();

    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-11-22 11:17

    The possible reason is the context of the alert dialog. You may be finished that activity so its trying to open in that context but which is already closed. Try changing the context of that dialog to you first activity beacause it won't be finished till the end.

    e.g

    rather than this.

    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    

    try to use

    AlertDialog alertDialog = new AlertDialog.Builder(FirstActivity.getInstance()).create();
    
    0 讨论(0)
  • 2020-11-22 11:18

    I had dialog showing function:

    void showDialog(){
        new AlertDialog.Builder(MyActivity.this)
        ...
        .show();
    }
    

    I was getting this error and i just had to check isFinishing() before calling this dialog showing function.

    if(!isFinishing())
        showDialog();
    
    0 讨论(0)
提交回复
热议问题