如何设置不在contentView中的ImageView图像?

时间:2015-08-12 01:55:38

标签: android android-imageview android-bitmap

我似乎无法从不同的布局设置ImageView。在下面的课程中,我使用一个具有ImageView的布局,我可以将ImageView设置为用户从库中选择的图像。但是,当我设置ImageView不在我的setContentView时,我会收到NullPointerException错误。 无论如何还有解决方法吗?我尝试使用BitmapUtility,但这也不起作用。求救!

@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;
    }
 }

0 个答案:

没有答案