NullPointer when dynamically creating TableLayout in Android -
i trying add table view in fragment getting nullpointer
when try add row layout. tl.addview(tr_headings);
looks causing crash.
thanks looking @ this!
public class historyfragment extends fragment { arraylist<workouts> workout_list; tablelayout tablelayout; public historyfragment() { // required empty public constructor } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { string name = "bench press"; string reps = "10"; string weight = "150"; string comment = "good weight. last rep tough"; simpledateformat df = new simpledateformat("yyyy/mm/dd hh:mm"); date d = new date(); string date = df.format(d); workout_list = new arraylist<workouts>(); for(int = 0; < 10; i++){ workouts workout = new workouts(name, reps, weight, comment, date); workout_list.add(workout); } view rootview = inflater.inflate(r.layout.fragment_log, container, false); tablelayout tl = (tablelayout) rootview.findviewbyid(r.id.historytable); tablerow tr_headings = new tablerow(getactivity()); textview name_heading = new textview(getactivity()); textview reps_heading = new textview(getactivity()); textview weight_heading = new textview(getactivity()); textview date_heading = new textview(getactivity()); textview comment_heading = new textview(getactivity()); name_heading.settext("exercise name"); reps_heading.settext("reps"); weight_heading.settext("weight"); date_heading.settext("date"); comment_heading.settext("comment"); tr_headings.addview(name_heading); tr_headings.addview(reps_heading); tr_headings.addview(weight_heading); tr_headings.addview(date_heading); tr_headings.addview(comment_heading); tl.addview(tr_headings); //null pointer here! for(int j = 0; j < workout_list.size(); j++){ textview tv_name = new textview(getactivity()); textview tv_reps = new textview(getactivity()); textview tv_weight = new textview(getactivity()); textview tv_date = new textview(getactivity()); textview tv_comment = new textview(getactivity()); tv_name.settext(workout_list.get(j).getexercisename()); tv_reps.settext(workout_list.get(j).getnumreps()); tv_weight.settext(workout_list.get(j).getweight()); tv_date.settext(workout_list.get(j).getdate()); tv_comment.settext(workout_list.get(j).getcomment()); tablerow tr = new tablerow(getactivity()); tr.addview(tv_name); tr.addview(tv_reps); tr.addview(tv_weight); tr.addview(tv_date); tr.addview(tv_comment); tl.addview(tr); } // inflate layout fragment return rootview; } }
Comments
Post a Comment