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

在OnCreate方法中调用

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

时,会出现以下错误:

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()的了,但是它还存在,所以回有上面的提示。
有两种解决办法:

(1)在onPause()中将它dismiss()了。



@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:


 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); 
                    }
                });

你可能感兴趣的:(android,PopupWindow,has,leaked)