Android Listview - Null Pointer Exception -
when trying display items on listview
showing nullpointerexception
. in think got null either position or convertview , textview
id , linearlayout
id both correct. below lines got exception in code.
viewitem.ll.setbackgroundcolor(rainbow[position]); viewitem.time.settext(timevalueslist.get(position).getelapsetime().replace(" minutes", ""));
here mycode :
class timerlistadapter extends baseadapter{ layoutinflater li; context ct; public timerlistadapter(context c){ this.ct=c; } @override public int getcount() { // todo auto-generated method stub return timevalueslist.size(); } @override public object getitem(int position) { // todo auto-generated method stub return position; } @override public long getitemid(int position) { // todo auto-generated method stub return position; } @override public view getview(final int position, view convertview, viewgroup arg2) { li=(layoutinflater)ct.getsystemservice(layout_inflater_service); final viewitem viewitem; if (convertview == null) { viewitem = new viewitem(); convertview=li.inflate(r.layout.salontimer_listitem, null); viewitem.time=(textview)convertview.findviewbyid(r.id.elapse_time); viewitem.ll=(linearlayout)convertview.findviewbyid(r.id.llayout); viewitem.playnpause=(imageview)convertview.findviewbyid(r.id.play_pause_time); viewitem.addtime=(imageview)convertview.findviewbyid(r.id.add_time); viewitem.deletetime=(imageview)convertview.findviewbyid(r.id.delete_time); viewitem.sharetime=(imageview)convertview.findviewbyid(r.id.share_time); }else{ viewitem = (viewitem) convertview.gettag(); } int[] rainbow = context.getresources().getintarray(r.array.rainbow); viewitem.ll.setbackgroundcolor(rainbow[position]); viewitem.time.settext(timevalueslist.get(position).getelapsetime().replace(" minutes", "")); viewitem.playnpause.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { settime(viewitem.time,viewitem.time.gettag().tostring(),integer.parseint(timevalueslist.get(position).getelapsetime().replace(" minutes", ""))*60*1000); } }); viewitem.addtime.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { } }); viewitem.deletetime.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { sdb.deletesalontime(timevalueslist.get(position).getid()); timevalueslist.remove(position); tadapter.notifydatasetchanged(); } }); viewitem.sharetime.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { } }); return convertview; } protected void settime(final textview tv, string tag,int time) { if (tv.gettag().tostring().equals(tag)) { countdowntimer ct = new countdowntimer(time, 1000) { //the timer runs 30 seconds in case public void ontick(long millisuntilfinished){ int seconds = (int) ((millisuntilfinished) / 1000) % 60 ; int minutes = (int) (((millisuntilfinished ) / (1000*60)) % 60); string min = integer.tostring(minutes); string sec = integer.tostring(seconds); tv.settext(min+":"+sec); } @override public void onfinish() { } }.start(); } } }
if convertview
not null, retreive viewitem
tag. have never set tag before.
to fix add line @ end if (convertview == null)
block:
convertview.settag(viewitem);
Comments
Post a Comment