java - Change ImageResource from a different activity in Android -


i need setimageresource in activity a's imageview after clicking button in activity b. i'm trying creating public static imageview.

here's code of activity a:

public class activitya extends activity {  public static imageview image;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_activitya);      image = (imageview) findviewbyid(r.id.image);  } 

here's code of activity b:

button.setonclicklistener(new onclicklistener() {           @override         public void onclick(view button) {              if(edittext.gettext().tostring().equalsignorecase("mytext")) {                 activitya.image.setimageresource(r.drawable.other_image);              intent act2= new intent(activityb.this,activitya.class);                 startactivity(act2);                 }             });  } 

i can't understand why app crashes after button clicked (it works if remove "activitya.image.setimageresource(r.id.other_image)" ) problem must here. should write else in activitya.class? thank every possible solution , sorry bad english.

you can this:

public class activitya extends activity {  public static imageview image;   @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_activitya);  image = (imageview) findviewbyid(r.id.image); image.setimageresource(getintent().getintextra("myimageresource",r.drawable.default_image); } 

*note: used r.drawable.default_image in case "myimageresource" not found

button.setonclicklistener(new onclicklistener() {       @override     public void onclick(view button) {           intent act2= new intent(activityb.this,activitya.class);             act2.putextra("myimageresource", r.drawable.other_image);             startactivity(act2);             }         });   } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -