WindowManagerBadTokenException unable to add window

前端 未结 1 407
悲哀的现实
悲哀的现实 2021-01-24 04:49

I have an error below:

  //TODO tryAgain
private void tryAgain(){
    new AlertDialog.Builder(this)
    .setTitle(\"Game Over\")
    .setMessage(\"Times Up!\")
          


        
相关标签:
1条回答
  • 2021-01-24 05:11

    This can occur when you are showing the dialog for a context that no longer exists. When you are calling show() of dialog the activity is destroyed. This happens generally after async operations. Make sure that your activity(in which you want to show dialog) is running.

    Don't call the activity after finishing it just create a new intent and start the activity. Do like this:

    private void tryAgain(){
        new AlertDialog.Builder(this)
        .setTitle("Game Over")
        .setMessage("Times Up!")
        .setPositiveButton("Try Again?", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                dialog.cancel();
                Intent retry = new Intent(this, Stage1_3.class);//I guess Stage1_3 is the current activity
                finish();
                startActivity(retry);
            }
         })
        .setNegativeButton("Back to Menu", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                dialog.cancel();
                Intent i = new Intent(Stage1_3.this, ShapingColors.class);
                finish();
                startActivity(i);
            }
         })
         .show();   
    }   
    
    0 讨论(0)
提交回复
热议问题