android - Getting NULL Pointer Exception while updating a Custom List View in Main Activity -
update 2
i have 3 class basically.for custom list view in main view.the problem while opening main activity getting null pointer exception , list not coming.first in main xml file.
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" >
<button android:text="take (small) picture" android:id="@+id/btnintends" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/pic" android:orientation="vertical" > <textview android:layout_width="match_parent" android:layout_height="match_parent" android:text="choose picture or video share" android:gravity="center_horizontal" android:textsize="18dp" />
<linearlayout android:layout_width="match_parent" android:id="@+id/imageview" android:layout_height="300dp" android:layout_gravity="center_vertical" android:orientation="vertical" > <imageview android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_margintop="200dp" android:visibility="visible" android:id="@+id/imageview1" />
<linearlayout android:layout_below="@id/imageview" android:layout_height="wrap_content" android:layout_width = "match_parent" android:orientation="horizontal"> <listview android:id="@+id/list" android:layout_width="wrap_content" android:layout_height="match_parent" android:visibility="visible" /> </linearlayout> </relativelayout>
here list_item.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <textview android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_torightof="@+id/icon" android:paddingbottom="10dp" android:textcolor="#cc0033" android:textsize="16dp" /> <textview android:id="@+id/desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/title" android:layout_torightof="@+id/icon" android:paddingleft="10dp" android:textcolor="#3399ff" android:textsize="14dp" /> </relativelayout>
as can see here xml have 2 buttons,one imageview , list view.now list gets updated main activity getting 2 things. 1.)a small text 2.)a large text
now in main activity oncreate method gets these 2 things local database.the data coming can see in mylogcat.so there no problem regarding database.so here goes oncreate method mainactivity.
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.picactivity); mydb = new dbhelper(picactivity.this); try { mydb.createdatabase(cont); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } system.out.println("i in picactivity"); mimageview = (imageview) findviewbyid(r.id.imageview1); mimagebitmap = null; button picbtn = (button) findviewbyid(r.id.btnintend); setbtnlistenerordisable( picbtn, mtakepiconclicklistener, mediastore.action_image_capture ); button picsbtn = (button) findviewbyid(r.id.btnintends); setbtnlistenerordisable( picsbtn, mtakepicsonclicklistener, mediastore.action_image_capture ); if (build.version.sdk_int >= build.version_codes.froyo) { malbumstoragedirfactory = new picalbumdirfactory(); } else { malbumstoragedirfactory = new basealbumdirfactory(); } mydb.opendatabase(); images = mydb.getimages(); description = mydb.getdescriptions(); location = mydb.getlocations(); int = images.size(); system.out.println("my database size"+i); int k = 0; if(i>0){ rowitems = new arraylist<rowitem>(); (int j = 0;j<i ; j++) { system.out.println("loop value:--"+j); system.out.println("location details:-----"+location.get(j)); system.out.println("description details:------"+description.get(j)); rowitem item = new rowitem(location.get(j), description.get(j)); rowitems.add(item); } listview = (listview) findviewbyid(r.id.list); customlistviewadapter adapter = new customlistviewadapter(picactivity.this, r.layout.list_item, rowitems); listview.setadapter(adapter); listview.setonitemclicklistener(this); }
now listview onitemclick listner method like
@override public void onitemclick(adapterview<?> parent, view view, int position, long id) { toast toast = toast.maketext(getapplicationcontext(), "item " + (position + 1) + ": " + rowitems.get(position), toast.length_short); toast.setgravity(gravity.bottom|gravity.center_horizontal, 0, 0); toast.show(); }
now there 2 other class.one rowitem class storing data row wise in list.that this
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; } }
and lastly customlistview class........
public class customlistviewadapter extends arrayadapter<rowitem> { context context; bitmap mybitmap; public customlistviewadapter(context context, int resourceid, list<rowitem> items) { super(context, resourceid, items); this.context = context; } 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; } }
lastly logcat view..
09-04 14:11:57.634: i/system.out(16814): context value:--com.cdotsys.photoapp.picactivity@421a24a0 09-04 14:11:57.639: i/system.out(16814): storage state:-mounted 09-04 14:11:57.639: i/system.out(16814): yes 09-04 14:11:57.639: i/system.out(16814): database path:--/storage/sdcard0/photoapp 09-04 14:11:57.639: i/system.out(16814): in picactivity 09-04 14:11:57.644: i/system.out(16814): database path:----/storage/sdcard0/photoapp/photodb 09-04 14:11:57.679: i/system.out(16814): database size1 09-04 14:11:57.684: i/system.out(16814): loop value:--0 09-04 14:11:57.684: i/system.out(16814): location details:-----vjgt 09-04 14:11:57.684: i/system.out(16814): description details:------gugf 09-04 14:11:57.714: d/androidruntime(16814): shutting down vm 09-04 14:11:57.714: w/dalvikvm(16814): threadid=1: thread exiting uncaught exception (group=0x417422a0) 09-04 14:34:57.834: e/androidruntime(18547): fatal exception: main 09-04 14:34:57.834: e/androidruntime(18547): java.lang.nullpointerexception 09-04 14:34:57.834: e/androidruntime(18547): @ com.cdotsys.adapters.customlistviewadapter.getview(customlistviewadapter.java:56) 09-04 14:11:57.764: e/androidruntime(16814): fatal exception: main 09-04 14:11:57.764: e/androidruntime(16814): java.lang.nullpointerexception 09-04 14:11:57.764: e/androidruntime(16814): @ java.io.file.fixslashes(file.java:185) 09-04 14:11:57.764: e/androidruntime(16814): @ java.io.file.<init>(file.java:134) 09-04 14:11:57.764: e/androidruntime(16814): @ android.widget.abslistview.obtainview(abslistview.java:2465) 09-04 14:11:57.764: e/androidruntime(16814): @ android.widget.listview.measureheightofchildren(listview.java:1250) 09-04 14:11:57.764: e/androidruntime(16814): @ android.widget.listview.onmeasure(listview.java:1162) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.view.measure(view.java:15504) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5059) 09-04 14:11:57.764: e/androidruntime(16814): @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1396) 09-04 14:11:57.764: e/androidruntime(16814): @ android.widget.linearlayout.measurehorizontal(linearlayout.java:1038) 09-04 14:11:57.764: e/androidruntime(16814): @ android.widget.linearlayout.onmeasure(linearlayout.java:576) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.view.measure(view.java:15504) 09-04 14:11:57.764: e/androidruntime(16814): @ android.widget.relativelayout.measurechild(relativelayout.java:602) 09-04 14:11:57.764: e/androidruntime(16814): @ android.widget.relativelayout.onmeasure(relativelayout.java:415) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.view.measure(view.java:15504) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5059) 09-04 14:11:57.764: e/androidruntime(16814): @ android.widget.framelayout.onmeasure(framelayout.java:310) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.view.measure(view.java:15504) 09-04 14:11:57.764: e/androidruntime(16814): @ android.widget.linearlayout.measurevertical(linearlayout.java:833) 09-04 14:11:57.764: e/androidruntime(16814): @ android.widget.linearlayout.onmeasure(linearlayout.java:574) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.view.measure(view.java:15504) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5059) 09-04 14:11:57.764: e/androidruntime(16814): @ android.widget.framelayout.onmeasure(framelayout.java:310) 09-04 14:11:57.764: e/androidruntime(16814): @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2385) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.view.measure(view.java:15504) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:1979) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1222) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1395) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1115) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:4526) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.choreographer$callbackrecord.run(choreographer.java:725) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.choreographer.docallbacks(choreographer.java:555) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.choreographer.doframe(choreographer.java:525) 09-04 14:11:57.764: e/androidruntime(16814): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:711) 09-04 14:11:57.764: e/androidruntime(16814): @ android.os.handler.handlecallback(handler.java:615) 09-04 14:11:57.764: e/androidruntime(16814): @ android.os.handler.dispatchmessage(handler.java:92) 09-04 14:11:57.764: e/androidruntime(16814): @ android.os.looper.loop(looper.java:137) 09-04 14:11:57.764: e/androidruntime(16814): @ android.app.activitythread.main(activitythread.java:4921) 09-04 14:11:57.764: e/androidruntime(16814): @ java.lang.reflect.method.invokenative(native method) 09-04 14:11:57.764: e/androidruntime(16814): @ java.lang.reflect.method.invoke(method.java:511) 09-04 14:11:57.764: e/androidruntime(16814): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1027) 09-04 14:11:57.764: e/androidruntime(16814): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:794) 09-04 14:11:57.764: e/androidruntime(16814): @ dalvik.system.nativestart.main(native method)
here can see there no problem regarding database.there problem regarding upgrading views guess.please me.please !! how can rid of null pointer exception happening in main thread.kindly please me.its quite bothering me quite amount of time :(
please check @override public int getcount() { if (arr == null ) return 0; return arr.size(); } @override public object getitem(int arg0) { if (arr == null) return null; else return arr.get(arg0); } , check null value in getview
Comments
Post a Comment