android - How to set image for ImageView? -


i trying make imageview swiping images on touch motion, when motion next image, got error saying:

java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.imageview.setbackground(android.graphics.drawable.drawable)' on null object reference @ com.semicolon.www.slider.mainactivity.ontouchevent(mainactivity.java:48)

here code:

public class mainactivity extends actionbaractivity {      imageswitcher switch;     imageview image;     float intialx;     private cursor cursor;     private int columnindex, position = 0;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          switch = (imageswitcher) findviewbyid(r.id.imageswitcher);         image = (imageview) findviewbyid(r.id.image);     }      public boolean ontouchevent(motionevent event){          switch (event.getaction()) {              case motionevent.action_down:                 intialx = event.getx();             break;             case motionevent.action_up:                 float finalx = event.getx();                 if (intialx > finalx) {                     drawable r = getresources().getdrawable(r.drawable.img2);                     image.setbackground(r);                 }             break;         }          return false;     } } 

so how can fix error , put image (or background) imageview?

try using viewpager instead of imageswitcher

http://developer.android.com/reference/android/support/v4/view/viewpager.html