android - listview adapter doesnt want to set text -
ok trying simple listview filling array adapter have:
public class mainactivity extends activity {
string[] dish = {"soup", "rice", "grecha", "potato"}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.lunch); setrequestedorientation(activityinfo.screen_orientation_landscape); setcontentview(r.layout.lunch); listview lunchlv = (listview)findviewbyid(r.id.lunchlv); cafeadapter ca = new cafeadapter(); lunchlv.setadapter(ca); } public class cafeadapter extends baseadapter { @override public int getcount() { return dish.length; } @override public object getitem(int i) { return null; } @override public long getitemid(int i) { return 0; } @override public view getview(int i, view view, viewgroup viewgroup) { view = getlayoutinflater().inflate(r.layout.list, null); textview title = (textview) findviewbyid(r.id.title); title.settext(dish[i]); return view; } }
}
i have list.xml file , lunch.xml file
for weird reasons title.settext(dish[i]); produces error , makes program crash. have element id "title in xml file anyways java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settext(java.lang.charsequence)' on null object reference.
do have ideas doing wrong?
you should use
textview title = (textview) view.findviewbyid(r.id.title);
instead:
textview title = (textview) findviewbyid(r.id.title);
Comments
Post a Comment