java - Null pointer exception on list view -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i creating dynamic views. want add views list , remove them onactivityresult.
but getting nullpointerexception on list view when adding view list.
where should add view in list view?
here code :
public class mon extends fragment { private framelayout fab; private eventtablehelper mdb; private intent i; private viewgroup dayplanview; private int minutesfrom,minutesto; private list<eventdata> events; private list<view> list; private eventdata e; private layoutinflater inflater; public boolean editmode; private relativelayout container; relativelayout parent; view eventview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); inflater = (layoutinflater) getactivity().getsystemservice(context.layout_inflater_service); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_mon, container, false); fab = (framelayout) view.findviewbyid(r.id.main_fab); imagebutton imagebutton = (imagebutton) view.findviewbyid(r.id.imgbtn_fab); imagebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { = new intent(getactivity(), addeventactivity.class); editmode = false; i.putextra("editmode", editmode); startactivityforresult(i, 1); } }); dayplanview = (viewgroup) view.findviewbyid(r.id.hoursrelativelayout); showevents(); return view; } private void createevent(layoutinflater inflater, viewgroup dayplanview, int fromminutes, int tominutes, string title,string location,final int id) { eventview = inflater.inflate(r.layout.event_view, dayplanview, false); relativelayout.layoutparams layoutparams = (relativelayout.layoutparams) eventview.getlayoutparams(); container = (relativelayout) eventview.findviewbyid(r.id.container); textview tvtitle = (textview) eventview.findviewbyid(r.id.textviewtitle); if (tvtitle.getparent() != null) ((viewgroup) tvtitle.getparent()).removeview(tvtitle); if(location.equals("")) { tvtitle.settext("event : " + title); } else { tvtitle.settext("event : " + title + " (at : " + location +")"); } int distance = (tominutes - fromminutes); layoutparams.topmargin = dptopixels(fromminutes + 9); layoutparams.height = dptopixels(distance); eventview.setlayoutparams(layoutparams); dayplanview.addview(eventview); container.addview(tvtitle); eventview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { = new intent(getactivity(), addeventactivity.class); editmode = true; i.putextra("editmode", editmode); i.putextra("id", id); startactivityforresult(i, 1); } }); } @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); removeview(); showevents(); } public void showevents() { mdb = new eventtablehelper(getactivity()); events = mdb.getallevents("mon"); (eventdata eventdata : events) { int id = eventdata.getid(); string datefrom = eventdata.getfromdate(); if (datefrom != null) { string[] times = datefrom.substring(11, 16).split(":"); minutesfrom = integer.parseint(times[0]) * 60 + integer.parseint(times[1]); } string title = eventdata.gettitle(); string location = eventdata.getlocation(); string dateto = eventdata.gettodate(); if (dateto != null) { //times = dateto.substring(11,16).split(":"); string[] times1 = dateto.substring(11, 16).split(":"); minutesto = integer.parseint(times1[0]) * 60 + integer.parseint(times1[1]); } createevent(inflater, dayplanview, minutesfrom, minutesto, title, location, id); id++; list.add(eventview); } } public void removeview() { for(int i=0; i<list.size(); i++) { dayplanview.removeview(eventview); } } private int dptopixels(int dp) { return (int) (dp * getresources().getdisplaymetrics().density); } }
getting exception @ list.add(eventview); please help..
your list never initialized , when come results trying add element null referenced list
solution:
in oncreate
do:
list = new arraylist<view>();
Comments
Post a Comment