android.view.WindowLeaked的解決方案

來源:互聯網
上載者:User

標籤:android   style   http   color   cti   linux   


Android.view.WindowLeaked一般會發生在Activity中Dialog的顯示。

android的dialog建立的時候會傳入一個參數this,就是當前的activity,dialog必須在Activity銷毀之前放掉,否則就會報windowleaked的錯誤。

分析這個原因是:

Dialog是基於Activity而建立的:new ProgressDialog(this);this 就是Activity。Activtity先finish,那Dialog就沒得依附了,所以就會報android.view.WindowLeaked。

Android的每一個Activity都有個WindowManager表單管理器,同樣,構建在某個Activity之上的對話方塊、PopupWindow也有相應的WindowManager表單管理器。因為對話方塊、PopupWindown不能脫離Activity而單獨存在著,所以當某個Dialog或者某個PopupWindow正在顯示的時候我們去finish()了承載該Dialog(或PopupWindow)的Activity時,就會拋Window Leaked異常了,因為這個Dialog(或PopupWindow)的WindowManager已經沒有誰可以附屬了,所以它的表單管理器已經泄漏了。

所以在finish之前把dialog給dismiss掉就行了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.