android - startActivity() is not working with Custom ListView's ItemClick? -
i new android app. working on app needs display image , text in listview. had extend listactivity. in had used efficientadapter extending baseadapter. had setted onitemclicklistner too. when try open new screen while clicking on tany item app trow nullpointer exception in intent , startactivity method.
public class myclass extends listactivity { public void oncreate(bundle icicle) { super.oncreate(icicle); setlistadapter(new stationadapter(this)); .. } private static class stationadapter extends baseadapter { static layoutinflater myinflater; public stationadapter(context context) { myinflater = layoutinflater.from(context); } public int getcount() { return myarray.length; } public object getitem(int position) { return position; } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if(convertview == null) { holder = new viewholder(); convertview = myinflater.inflate(r.layout.customlister, null); holder.title = (textview)convertview.findviewbyid(r.id.mytitle); holder.icon = (imageview)convertview.findviewbyid(r.id.myicon); } else { holder = (viewholder)convertview.gettag(); } holder.title.settext(myarray[position]); holder.icon.setimagebitmap(iconimages[position]); convertview.setonclicklistener(new onitemclicklistener(position)); return convertview; } static class viewholder { textview title; imageview icon; } private class onitemclicklistener implements onclicklistener { private int mposition; onitemclicklistener(int position) { mposition = position; } public void onclick(view view) { try { system.out.println("clicked on row - = " + mposition); intent = new intent(mycurrentclass.this,mynewclass.class); startactivity(i); } catch (exception e) { system.out.println("xml pasing excpetion = " + e); } } } } } the following 2 lines:
intent = new intent(mycurrentclass.this,mynewclass.class); startactivity(i); works fine if directly write these in class extend listactivity. not working here. had tried set intent in oncreate method , create function:
public void gotonextscreen() { try { startactivity(i); } catch (exception e) { system.out.println("xml parsing exception = " + e); } } then in onitemclicklistener implementation had called method still exception. please me out how can move on new screen. appreciated.
thanks in advance.
the reason getting nullpointerexception on startactivity(i) because method not defined in private class onitemclicklistener implements onclicklistener. works fine in myclass because startactivity defined in activity class (from listactivity exends from).
to make current code work following:
intent = new intent(myclass.this,mynewclass.class); myclass.this.startactivity(i); as startactivity defined in activity class (from listactivity exends from), need reference class able perform method inner classes.
hope helps!
p.s. noticed have typo in code, have myinflater , myinflater in stationadapter.
Comments
Post a Comment