使用 PopupWindow 的時候遇到Activity com.avcit… has leaked

在OnCreate方法中調用java

popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, x, y);android

時,會出現如下錯誤:app

Activity com.avcit.conference.MainActivity has leaked window android.widget.PopupWindow$PopupViewContainer@406dfc10 that was originally added here  

android.view.WindowLeaked: Activity com.avcit.conference.MainActivity has leaked window android.widget.PopupWindow$PopupViewContainer@406dfc10 that was originally added here

這是由於這個popupWindow依賴的父Activity已經finish()的了,可是它還存在,因此回有上面的提示。 有兩種解決辦法:ide

(1)在onPause()中將它dismiss()了。post

<!-- lang: java -->

@Override
	    protected void onPause() {
	    	// TODO Auto-generated method stub
	    	super.onPause();
	    	dismissPopupWindow();
	    	//if(popupWindow != null)
	    }
      
   //判斷PopupWindow是否是存在,存在就把它dismiss掉  
    private void dismissPopupWindow() {

    	if(popupWindow != null){
    		popupWindow.dismiss();
    		popupWindow = null;
    	}
	}

(2)從新使用一個線程來跑這個popupWindow:動畫

<!-- lang: java -->
 handler.post(new Runnable() {
					@Override
					public void run() {
						int [] location = new int[2];
						view.getLocationInWindow(location);
						View popupView = View.inflate(AppManagerActivity.this, R.layout.popup_item, null);
						LinearLayout ll_app_uninstall = (LinearLayout) popupView.findViewById(R.id.ll_app_uninstall);  
			            LinearLayout ll_app_run = (LinearLayout) popupView.findViewById(R.id.ll_app_start);  
			            LinearLayout ll_app_share = (LinearLayout) popupView.findViewById(R.id.ll_app_share);  
			            ll_app_run.setOnClickListener(AppManagerActivity.this);
			            ll_app_uninstall.setOnClickListener(AppManagerActivity.this); 
			            ll_app_share.setOnClickListener(AppManagerActivity.this); 
			            
			            //拿到當前點擊的條目,並設置到view裏面
			            AppInfo info = (AppInfo) lv_app_manager.getItemAtPosition(position);
			            ll_app_uninstall.setTag(info);  
		                ll_app_run.setTag(info);  
		                ll_app_share.setTag(info); 
		                
		                //添加動畫  
		                LinearLayout ll_app_popup = (LinearLayout) popupView.findViewById(R.id.ll_app_popup);  
		                ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);  
		                scaleAnimation.setDuration(300);  
						 //new 一個PopupWindow出來  
		                popupWindow = new PopupWindow(popupView, 230, 70);  
		                //必定要給PopupWindow設置一個背景圖片,否則的話,會有不少未知的問題的  
		                //如沒辦法給它加上動畫,還有顯示會有問題等,  
		                //若是咱們沒有要設置的圖片,那麼咱們就給它加上了一個透明的背景圖片  
		                Drawable drawable = new ColorDrawable(Color.TRANSPARENT);  
		                popupWindow.setBackgroundDrawable(drawable);  
		                
		                int x = location[0] + 60;  
		                int y = location[1];  
		                //把PopupWindow顯示出來  
		                popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, x, y);  
		                //開啓動畫 
		                ll_app_popup.startAnimation(scaleAnimation); 
					}
				});
相關文章
相關標籤/搜索