我正在尝试使用一些按钮实现弹出窗口。我在单击按钮时打开弹出窗口时遇到E / WindowManager:android.view.WindowLeaked:活动的错误。这是我的活动代码。此行的错误= 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();
        }
    }


这是我的日志猫错误

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)

最佳答案


pwindo.dismiss()在Onclick事件中。

请参见下面的帮助链接:http://www.androidhub4you.com/2012/07/how-to-create-popup-window-in-android.html

07-26 05:42