Is there a way that I can change ImageView of Main.class class through Gallery.class?
my Main class onClick calls the method setImage of Gallery.class
public void setImage(int currentView) {
Log.d("fgdsf","dumaan dito");
this.currentView = currentView;
Intent gallery = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gallery, RESULT_LOAD_IMAGE);
}
and this is the onActivityResult
@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 };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
LayoutInflater inflater = (LayoutInflater) mActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.activity_main, null);
if (currentView == 0) {
ImageView imageView = (ImageView) v
.findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
if(currentView==1)
{
ImageView imageView = (ImageView) v
.findViewById(R.id.imageView2);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
and I get this error
08-07 08:35:08.208: E/AndroidRuntime(15689): Caused by: java.lang.NullPointerException 08-07 08:35:08.208: E/AndroidRuntime(15689): at android.app.Activity.startActivityForResult(Activity.java:3370)
I don't think you can. You can only control the layout of your current activity, along with its components. Since the component isn't on the view of your current Activity (in your case is Gallery), accessing it will throw the NullPointerException. What you can do is, pass the path of the selected image on you Gallery to the Main class then retrieve the image path and set it to your ImageView.