0

I can't seem to set the ImageView from a different layout. In the class below, I use one layout that has an ImageView, and I can set that ImageView to the image picked by the user from the gallery perfectly. But when I set an ImageView that's not in my setContentView I keep getting a NullPointerException error. Is there anyway workaround for this? I tried using BitmapUtility and that doesn't work either. Help please!

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.card_create_layout);
}

private void grabImage()
{
    Intent imageGetter = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(imageGetter, RESULT_LOAD_IMAGE);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
    {
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};//Array size of 1, and we put in a string
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        user_image_path = cursor.getString(columnIndex);//here we have our image path.
        cursor.close();


        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(user_image_path));//THIS WORKS PERFECTLY FINE, because, R.id.imageView is in the R.layout.card_create_layout (above).

       ImageView v = (ImageView) findViewById(R.id.maxCard);
        v.setImageBitmap(BitmapFactory.decodeFile(user_image_path));
        //NOW THIS DOESN'T WORK, I KEEP GETTING NULL POINT EXCEPTION, because R.id.maxCard(which is another ImageView in another layoutfile).
    }
}


@Override
public void onClick(View v)
{
    switch (v.getId())
    {
        case R.id.theCreateButton:
            grabImage();
            break;
    }
 }
Artem Mostyaev
  • 3,874
  • 10
  • 53
  • 60
TheQ
  • 1,949
  • 10
  • 38
  • 63
  • You cant access the other ImageView because its not present in the current view that you're using. When you do `findViewById()` it only looks in the current activity. – Antrromet Aug 12 '15 at 02:00
  • So is there a way where I can set the Image of another layout file? – TheQ Aug 12 '15 at 02:06
  • But why do you want to do that? Because in any case that another layout file is not visible to the user at the moment. – Antrromet Aug 12 '15 at 02:06
  • Basically what I'm trying to do is have a Tinder like card. So in one activity, where there's a "select image" button, where you can select an image from gallery, I want that image they select to go to the Tinder card layout and add that image to the tinder card. Then I can call that card later in another activity to be viewed by the user. – TheQ Aug 12 '15 at 02:37
  • Why not just keep the image path in your app preferences and load when needed? What you're trying to do doesn't make any sense. – JanithaR Aug 12 '15 at 07:41

0 Answers0