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