title: Assigning ID to specific dynamically generated content
body: <p>I have a template form that is called and put into view programmatically. In the form I have a button that enables the user to search the phone for images and then saves the image. The image thumbnail shows the image selected and saved. The thumbnail is placed in an ImageView with an XML id of "imgView" next to the button. In order to save the image for each dynamic form generated I am, on form creation/retrieval, selecting the "imgView" generated and then assigning it an integer id which starts from 50001, like so:</p>
  
  <pre><code>case R.id.makeLayoutButton:
      v1 = vi.inflate(R.layout.form_template, null);
  
      //add view to the insertPoint
      ((LinearLayout) insertPoint).addView(v1);
  
      mStartActivityButton = (Button)v1.findViewById(R.id.start_file_picker_button1);
      mStartActivityButton.setOnClickListener(this);
  
      thumbnailContainer = (ImageView)v1.findViewById(R.id.imgView);
      thumbnailContainer.setId(setImageViewID);
      imageViewId = thumbnailContainer.getId();
      setImageViewID++;
  
      break;
  </code></pre>
  
  <p>I'm then, when the user selects an image, iterating from 50000 to 500020, then in the loop searching for an ImageView with an id of i, like so:</p>
  
  <pre><code>ImageView imageView = (ImageView)findViewById(i);
  </code></pre>
  
  <p>And when found, I'm placing the image in the appropriate imageView, like so:</p>
  
  <pre><code>if(i == imageView.getId()){
      imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
      return;
  }
  </code></pre>
  
  <p>Problem is that its crashing and giving a NULL POINTER EXCEPTION error when trying to retrieve and store the image. It seems like its not finding the ImageView with the appropriate ID assigned. Any clue as to where I'm going wrong?</p>
  
  <p>Code that starts image browsing:</p>
  
  <pre><code>case R.id.start_file_picker_button1:
  
  // Create a new Intent for the file picker activity
  Intent intent1 = new Intent(this, FilePickerActivity.class);
  Intent i1 = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  
  startActivityForResult(i1, RESULT_LOAD_IMAGE);
  break;
  </code></pre>
  
  <p>Code for that looks for image and stores it:</p>
  
  <pre><code>@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  
      if (requestCode == RESULT_LOAD_IMAGE &amp;&amp; resultCode == RESULT_OK &amp;&amp; null != data) {
          Uri selectedImage = data.getData();
          String[] filePathColumn = { MediaStore.Images.Media.DATA };
  
          Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
          cursor.moveToFirst();
  
  
          int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
          String picturePath = cursor.getString(columnIndex);
          cursor.close();
  
          for(int i = 50000;i&lt;=50020; i++){
              ImageView imageView = (ImageView)findViewById(i);
              if(i == imageView.getId()){
                  imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
                  return;
              }
          }
      }
  }
  </code></pre>