0

画像と2つのボタンを表示したいポップアップウィンドウがあります。ボタンは表示されますが、ImageView を使用しようとすると nullpointer 例外がスローされます。以下は、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 に答える