Activity has leaked window at alertDialog show() method

前端 未结 6 951
旧时难觅i
旧时难觅i 2020-12-14 16:23

I am getting window leak error at runtime because of using an AlertDialog.

I have pointed out the error line in the code below:

Stacktrace:<

相关标签:
6条回答
  • 2020-12-14 16:25

    You must need to dismiss/cancel your dialog once activity is finishing. It occurs usually if dialogs are not dismissed before Activity is ended.

    0 讨论(0)
  • 2020-12-14 16:26

    I don't know does this help but at me problem's were causing multidex. At build.gradle

    multiDexEnabled false
    
    0 讨论(0)
  • 2020-12-14 16:29

    You can also get a WindowLeaked exception if you're not in the UI thread when attempting to show the dialog. Easy fix:

    this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // show dialog here
        }
    });
    
    0 讨论(0)
  • 2020-12-14 16:33

    You get error because ProgressDialog is running while your Activity is destroyed. You should dismiss dialog before starting new Activity.

    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int id) {
          if (alertDialog != null && alertDialog.isShowing()) {
              alertDialog.dismiss();
          }
          Intent i = new Intent(FirstActivity.this, SecondActivity.class);
          startActivity(i);
      }
    });
    

    I hope it helps!

    0 讨论(0)
  • 2020-12-14 16:33

    Dismiss the alertDialog in onPause() method. i.e. call alertDialog.dismiss()

    Note : WindowLeaked exceptions are occured usually if dialogs are not dismissed before Activity is ended.

    0 讨论(0)
  • 2020-12-14 16:35

    Check in your manifest if you have setted internet permissions:

    <uses-permission android:name="android.permission.INTERNET" />
    
    0 讨论(0)
提交回复
热议问题