Android:WindowManager:android.view.WindowLeaked:Activity

时间:2015-10-30 04:32:51

标签: android

我尝试使用一些按钮实现弹出窗口。我在打开弹出窗口时收到 E / WindowManager:android.view.WindowLeaked:Activity 的错误按钮上的窗口单击。这是我的活动代码。此行的错误= pwindo.showAtLocation(layout,Gravity.BOTTOM,0,0);

private void initiatePopupWindow()
    {
        try
        {
            LayoutInflater inflater = (LayoutInflater) AddPost.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.camera_popup, (ViewGroup) findViewById(R.id.popup_element));
            pwindo = new PopupWindow(layout, 700, 380, true);
            pwindo.showAtLocation(layout, Gravity.BOTTOM, 0, 0);
            pwindo.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

            btnCamera = (Button) layout.findViewById(R.id.button_Camera);
            btnCamera.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(cameraIntent, CAMERA_REQUEST);
                }
            });

            btnGallery = (Button) layout.findViewById(R.id.button_Gallery);
            btnGallery.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);

                   }
            });

            cancelPopUp = (Button) layout.findViewById(R.id.btnCancelCamera);
            cancelPopUp.setOnClickListener(cancel_button_click_listener);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这是我的日志cat错误

10-30 09:56:35.860    6582-6582/? E/WindowManager﹕ android.view.WindowLeaked: Activity com.example.tazeen.classnkk.AddPost has leaked window android.widget.LinearLayout{1cc3bcc6 V.E..... ........ 0,0-700,380 #7f0c0093 app:id/popup_element} that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.widget.PopupWindow.invokePopup(PopupWindow.java:1058)
            at android.widget.PopupWindow.showAtLocation(PopupWindow.java:887)
            at android.widget.PopupWindow.showAtLocation(PopupWindow.java:851)
            at com.example.tazeen.classnkk.AddPost.initiatePopupWindow(AddPost.java:532)
            at com.example.tazeen.classnkk.AddPost.access$000(AddPost.java:80)
            at com.example.tazeen.classnkk.AddPost$5.onClick(AddPost.java:310)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19748)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

2 个答案:

答案 0 :(得分:1)

放 Onclick事件中的pwindo.dismiss()

请参阅以下链接获取帮助:http://www.androidhub4you.com/2012/07/how-to-create-popup-window-in-android.html

答案 1 :(得分:0)

使用以下代码,将弹出窗口放在您想要的位置。

 protected void SortPopup(final Activity context) {//pass your activity
       Point p;
       int popupWidth = 190;
       int popupHeight = 200;

       // Inflate the popup_layout.xml
       LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.sortbyview);
       LayoutInflater layoutInflater = (LayoutInflater) context
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View layout = layoutInflater.inflate(R.layout.sorting_popup, viewGroup);
       // Creating the PopupWindow
       final PopupWindow popup = new PopupWindow(context);
       popup.setContentView(layout);
       popup.setWidth(popupWidth);
       popup.setHeight(popupHeight);
       popup.setFocusable(true);
       // Some offset to align the pop up a bit to the right, and a bit down, relative to button's position.
       int OFFSET_X = -115;
       int OFFSET_Y = 50;
       // Clear the default translucent background
       popup.setBackgroundDrawable(new BitmapDrawable());
       // Displaying the pop up at the specified location, + offsets.
       popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
       Button b1 = (Button) layout.findViewById(R.id.b1);
       Button b2 = (Button) layout.findViewById(R.id.b2);

        b1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {

            }
        });
        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

            }
        });
}