0

我有一个弹出窗口,我想显示一个图像和两个按钮。确实会出现按钮,但是当我尝试使用 ImageView 时会引发空指针异常。下面是我显示 PopUpWindow 的代码片段:

private void initiatePopupWindow() {
        try {
            //We need to get the instance of the LayoutInflater, use the context of this activity
            LayoutInflater inflater = (LayoutInflater) RestPageActivity.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //Inflate the view from a predefined XML layout
            View layout = inflater.inflate(R.layout.popup_details,
                    (ViewGroup) findViewById(R.id.popup_element));
            // create a 300px width and 470px height PopupWindow
            System.out.println("SELECTED INDEX" + selectedIndex + "***" + IDs.size());


            System.out.println("ITEM ID:" + IDs.get(selectedIndex) + "--------------");
            ImageView iv = (ImageView) findViewById(R.id.ivItem);

            if (StaticParameters.getCompleteData().getMenusHash().containsKey(IDs.get(selectedIndex))) {
                Menu m = StaticParameters.getCompleteData().getMenusHash().get(IDs.get(selectedIndex));
                byte[] imgBytes = FileHandler.readFile2(this, "items", m.getActiveImage());
                System.out.println("///////////////77" + imgBytes.length + "//" + m.getActiveImage());
                if (imgBytes != null && iv != null) {
                    Bitmap b = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);
                    iv.setImageBitmap(b);

                    Matrix mMatrix = new Matrix();
                    mMatrix.setRectToRect(new RectF(0, 0, iv.getMeasuredWidth(), iv.getMeasuredHeight()), new RectF(0, 0, 100, 100), Matrix.ScaleToFit.CENTER);
                } else
                    System.out.println("else geldi");

            } else if (StaticParameters.getCompleteData().getProductsHash().containsKey(IDs.get(selectedIndex))) {
                Product m = StaticParameters.getCompleteData().getProductsHash().get(IDs.get(selectedIndex));
                byte[] imgBytes = FileHandler.readFile2(this, "items", m.getActiveImage());
                System.out.println("///////////////77" + imgBytes.length + "//" + m.getActiveImage());
                if (imgBytes != null && iv != null) {
                    Bitmap b = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);
                    iv.setImageBitmap(b);

                    Matrix mMatrix = new Matrix();
                    mMatrix.setRectToRect(new RectF(0, 0, iv.getMeasuredWidth(), iv.getMeasuredHeight()), new RectF(0, 0, 100, 100), Matrix.ScaleToFit.CENTER);
                } else
                    System.out.println("else geldi");
            }

            pw = new PopupWindow(layout, 300, 470, true);
            // display the popup in the center
            pw.showAtLocation(layout, Gravity.CENTER, 0, 0);


            //mResultText = (TextView) layout.findViewById(R.id.server_status_text);
            Button cancelButton = (Button) layout.findViewById(R.id.cancelButton);
            //makeBlack(cancelButton);
            cancelButton.setOnClickListener(cancel_button_click_listener);
            Button approveButton = (Button) layout.findViewById(R.id.completeButton);
            //makeBlack(cancelButton);
            approveButton.setOnClickListener(approve_button_click_listener);

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

2 回答 2

1

如果您在活动中声明了一个属性

private ImageView iv;

你必须改变:

ImageView iv = (ImageView) findViewById(R.id.ivItem);

经过

iv = (ImageView) findViewById(R.id.ivItem); 你得到这个 nullPointer 异常是因为你在本地声明它就是你的问题所在

于 2012-05-19T18:03:22.930 回答
0

更改此行

ImageView iv = (ImageView) findViewById(R.id.ivItem);

ImageView iv = (ImageView) layout.findViewById(R.id.ivItem);
于 2015-06-30T07:05:49.237 回答