2012-05-19 28 views
0

我有一個彈出窗口,我想要顯示一個圖像和兩個按鈕。按鈕確實出現,但是當我嘗試使用它時,ImageView會拋出空指針異常。下面的代碼片段,我顯示PopUpWindow:Android的Null 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(); 
     } 
    } 
+0

你有一個屬性 私人ImageView的IV; ? –

回答

1

如果你的活動已聲明的特性

private ImageView iv; 

你必須改變:

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

通過

iv = (ImageView) findViewById(R.id.ivItem); 你得到這個nullPointer異常b ecause您是在本地聲明它這就是你的問題是什麼,我的事情

+1

現在我去殺了自己! OH SHT !!! – dramaticlook

0

改變這一行

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

ImageView iv = (ImageView) layout.findViewById(R.id.ivItem); 
您的活動