java - NullPointer Exception while setting custom listview adapter -
i have been searching around few hours solve problem can't find anything. getting nullpointerexception in line listview.setadapter(adapter);
xml :
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <listview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </linearlayout>
offlineactivity :
public class offlineactivity extends activity implements onitemclicklistener { string[] filename; string[] name; list<rowitem> rowitems; string[] dis; listview listview; bufferedreader br; inputstream in; string line; file file; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); file f = new file(environment.getexternalstoragedirectory()+"/android/data/com.example.test/database/"); f.mkdirs(); filename = f.list(); string[] nametemp = new string[filename.length + 1]; string[] distemp = new string[filename.length + 1]; if(filename[0] == null){ setcontentview(r.layout.empty); } else{ setcontentview(r.layout.offline); try{ for(int i=0; i<filename.length; i++){ file = new file(environment.getexternalstoragedirectory().tostring()+"/android/data/com.example.test/database/"+filename[i]); in = new fileinputstream(file); br = new bufferedreader(new inputstreamreader(in)); nametemp[i] = br.readline(); line = br.readline(); distemp[i] = br.readline(); } }catch(filenotfoundexception e){} catch(ioexception e){} name = nametemp; dis = distemp; setcontentview(r.layout.empty); rowitems = new arraylist<rowitem>(); (int = 0; < name.length; i++) { if(name[i]==null) break; rowitem item = new rowitem(name[i], dis[i]); rowitems.add(item); } listview = (listview) findviewbyid(r.id.list); customlistviewadapter adapter = new customlistviewadapter(this, r.layout.list_item, rowitems); listview.setadapter(adapter); listview.setonitemclicklistener(this); } } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { toast.maketext(this, "loading lyrics "+name[position]+"...", toast.length_short).show(); intent = new intent(this, showdataactivity.class); i.putextra("name", name[position]); i.putextra("filename", filename[position]); startactivity(i); } }
this code customlistviewadapter class :
public class customlistviewadapter extends arrayadapter<rowitem> { context context; public customlistviewadapter(context context, int resourceid, list<rowitem> items) { super(context, resourceid, items); this.context = context; } /*private view holder class*/ private class viewholder { textview txttitle; textview txtdesc; } public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; rowitem rowitem = getitem(position); layoutinflater minflater = (layoutinflater) context .getsystemservice(activity.layout_inflater_service); if (convertview == null) { convertview = minflater.inflate(r.layout.list_item, null); holder = new viewholder(); holder.txtdesc = (textview) convertview.findviewbyid(r.id.desc); holder.txttitle = (textview) convertview.findviewbyid(r.id.title); convertview.settag(holder); } else holder = (viewholder) convertview.gettag(); holder.txtdesc.settext(rowitem.getdesc()); holder.txttitle.settext(rowitem.gettitle()); return convertview; } }
here's code rowitem class:
public class rowitem { private string title; private string desc; public rowitem( string title, string desc) { this.title = title; this.desc = desc; } public string getdesc() { return desc; } public void setdesc(string desc) { this.desc = desc; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } @override public string tostring() { return title + "\n" + desc; } }
this logcat :
e/androidruntime(20534): java.lang.runtimeexception: unable start activity componentinfo{com.example.test/com.example.test.offlineactivity}: java.lang.nullpointerexception e/androidruntime(20534): @ com.example.test.offlineactivity.oncreate(offlineactivity.java:59) w/activitymanager(22626): force finishing activity com.example.test/.offlineactivity
my 59th line : listview.setadapter(adapter);
any solve appreciated
correct me if i'm wrong looks calling
setcontentview(r.layout.empty);
again before try initialize listview
. assuming r.layout.empty
doesn't contain listview
id
of list
return null
when try initialize listview
.
for reasons this, idea call setcontentview()
more once in same activity
. there better way, imho.
Comments
Post a Comment